1

I am trying to learn how to use Jasmine and Sinon for testing a Backbone application, and I was following this tutorial. Nevertheless, I ran into a problem that I don't know how to solve.

Most likely the solution is simple, but I need some guidance ...

In my project.spec.js file this is the code that is giving the problem:

it("should not save when name is empty", function() {
    var eventSpy = sinon.spy();
    this.project.bind("error", eventSpy);
    this.project.save({"name": ""});
    expect(this.eventSpy.calledOnce).toBeTruthy();
    expect(this.eventSpy.calledWith(
      this.project, 
      "cannot have an empty name"
    )).toBeTruthy();
});

And this is the specific error that can be seen in the browser:

Failing 1 spec
7 specs | 1 failing
Project model should not save when name is empty.
TypeError: Object #<Object> has no method 'spy'
TypeError: Object #<Object> has no method 'spy'
    at null.<anonymous> (http://localhost:8888/__spec__/models/project.spec.js:53:26)
    at jasmine.Block.execute (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:1024:15)
    at jasmine.Queue.next_ (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2025:31)
    at jasmine.Queue.start (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:1978:8)
    at jasmine.Spec.execute (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2305:14)
    at jasmine.Queue.next_ (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2025:31)
    at onComplete (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2021:18)
    at jasmine.Suite.finish (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2407:5)
    at null.onComplete (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2451:10)
    at jasmine.Queue.next_ (http://localhost:8888/__JASMINE_ROOT__/jasmine.js:2035:14)

In addition to the sinon.js library, I have installed the jasmine-sinon.js library (both are in the vendor/assets/javascripts folder and are included in the application.js file).

Thank you, Alexandra


plt.hist(hmag, 30, range=[6.5, 12.5], facecolor='gray', align='mid')
4

3 回答 3

6

当我从 GitHub 下载 sinon.js 文件(没有 Sinon 文件夹)时,我遇到了这个问题。我通过从http://sinonjs.org/下载库解决了这个问题

于 2013-04-25T08:23:08.197 回答
2

我将根据上面的评论线程将此作为答案发布。我们已经将问题缩小到sinon.spy()被调用的行,所以它不是特定于这个测试,而是如何加载 sinon。

我怀疑问题是你在 application.js 中包含了 sinon 和 jasmine-sinon,而它们真的应该放在 spec/javascripts/spec.js 中(格式相同)。尝试改变它,看看是否有任何改变。

更新:

根据下面的评论线程,似乎代码已到达该this.project.save(...)行,但验证不起作用:我知道这一点,因为如果您在控制台中收到 POST 错误,这意味着主干实际上发出了请求(它不应该有,因为名称为空)。所以你应该回去检查你实际测试的代码。

于 2012-08-26T01:00:00.700 回答
0

我知道这个线程很旧,但是我今天在阅读本教程时遇到了类似的问题http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html . 看起来 Backbone 进行了更改,并在提供无效模型数据时调用“无效”事件,而不是“错误”。

如果您遇到此错误,请尝试更改:

it("should not save when name is empty", function() {
    ...
    this.project.bind("error", eventSpy);
    ...
});

至:

it("should not save when name is empty", function() {
    ...        
    this.project.bind("invalid", eventSpy);
    ...
});

这为我解决了这个问题。

于 2014-04-17T21:29:40.707 回答