JavaScript:Jasmine 错误:预期 { 0 : HTMLNode, length : 1 } 为 '#containers'

标签 javascript jasmine

我花了很长时间试图解决这个问题。任何人都可以帮我解决这个问题。

我完全按照这个 YouTube 教程进行操作: http://www.youtube.com/watch?v=3Huh44nsZTw

当我完成输入时:

describe("Experimentation", function() {
var elem;

beforeEach(function() {
    elem = $('<div id="container"><p>Hello World</p></div>');
});

it("allows us to search with CSS selectors", function() {
    expect(elem).toBe('#containers');    // Problem happened here!!!!!!!
});});

输出结果:

Experimentation allows us to search with CSS selectors
Expected { 0 : HTMLNode, length : 1 } to be '#containers'.
Error: Expected { 0 : HTMLNode, length : 1 } to be '#containers'.
    at stack (http://localhost:8888/__jasmine__/jasmine.js:1293:17)
    at buildExpectationResult (http://localhost:8888/__jasmine__/jasmine.js:1270:14)
    at Spec.Env.expectationResultFactory (http://localhost:8888/__jasmine__/jasmine.js:484:18)
    at Spec.addExpectationResult (http://localhost:8888/__jasmine__/jasmine.js:260:46)
    at Expectation.addExpectationResult (http://localhost:8888/__jasmine__/jasmine.js:442:21)
    at Expectation.toBe (http://localhost:8888/__jasmine__/jasmine.js:1209:12)
    at Object.<anonymous> (http://localhost:8888/__spec__/experimentationSpec.js:9:16)
    at attemptSync (http://localhost:8888/__jasmine__/jasmine.js:1510:12)
    at QueueRunner.run (http://localhost:8888/__jasmine__/jasmine.js:1498:9)
    at QueueRunner.execute (http://localhost:8888/__jasmine__/jasmine.js:1485:10)

最佳答案

elem = $('<div id="container"><p>Hello World</p></div>');

expect(elem).toBe('#containers'); 

您正在将容器与容器进行比较。请注意额外的“s”。

关于JavaScript:Jasmine 错误:预期 { 0 : HTMLNode, length : 1 } 为 '#containers',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24845610/

相关文章:

javascript - 如何在 Angular Directive(指令)规范中使用 jasmine stub jquery 函数

Angular + (Jasmine/Karma) - 错误 : Illegal state: Could not load the summary for directive

javascript - 尝试使用 Jasmine spy 并收到 "is not a function"错误

javascript - 从文本区域执行代码

javascript - 覆盖不同页面上的javascript函数

javascript - 我可以在 MVC 项目中捆绑并缩小应用程序文件夹的 js 文件吗?

javascript - 编写可重用的 javascript 模块和库。

javascript - 如何使用 Karma 任务运行器使用 Jasmine 测试来模拟 Angular 应用程序中的 d3.json() 调用

javascript - 如何在 Jasmine 测试中接受 getUserMedia() 的权限?

javascript - 如何在导航悬停时在单独的 div 更改中制作图像?