Jasmine 2.0与Jasmine-Jquery的兼容性

时间:2014-01-13 21:33:11

标签: jquery testing jasmine jasmine-jquery

我在使用jasmine-jquery插件运行Jquery测试时遇到了困难。以下是我采取的步骤:

  1. gem install jasmine
  2. jasmine init
  3. 将jquery和jasmine-jquery添加到帮助文件夹
  4. 这是我的存储库https://github.com/shenst1/jasminesample
  5. 以下是我按照这些教程推荐的规范:https://www.youtube.com/watch?v=3Huh44nsZTw

    describe("My Feature", function() {
      it("should add numbers", function() {
        expect(1+1).toBe(2);
       });
     });
    describe("experimentation", function() {
      var elem;
      beforeEach(function() {
      elem = $('<div id="container"><p>Hello World</p></div>');
      });
      it("allows us to serach with Css selectors", function() {
       expect(elem).toBe('#container');
       expect(elem).toContainElement('p');
        expect(elem).toEqual('#container');
       expect(elem).toEqual('p')
      });
     });
    

    第一个规范按预期传递,但第二个规则使用jquery失败并出现以下错误:

        Expected { 0 : HTMLNode, length : 1 } to be '#container'.
        Error: Expected { 0 : HTMLNode, length : 1 } to be '#container'.
        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__/testSpec.js:12:18)
        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)
    TypeError: Object #<Expectation> has no method 'toContainElement'
    TypeError: Object #<Expectation> has no method 'toContainElement'
        at Object.<anonymous> (http://localhost:8888/__spec__/testSpec.js:13:18)
        at attemptSync (http://localhost:8888/__jasmine__/jasmine.js:1510:12)
    

    看起来jquery匹配器没有正确加载。它似乎是将对象转换为字符串而不是检查html元素。我尝试使用Support Jasmine v2.0(版本1.5.92)中的jasmine-jquery.js版本,但是失败并出现相同的错误。 我是否需要回滚jasmine版本以支持Jquery,或者我只是错过了一些关于如何使它与jquery一起工作的东西? 谢谢, 安德鲁

1 个答案:

答案 0 :(得分:3)

我维护jasmine jquery,我刚刚在几分钟之前添加了对jasmine v2的支持。 https://github.com/velesin/jasmine-jquery/