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


I'm going to post this as an answer, based on the comment thread above. We've narrowed down the problem to the line where sinon.spy() is called, so it's not specific to this test but to how sinon is being loaded.

I suspect the problem is that you're including sinon and jasmine-sinon in application.js, when they should really go in spec/javascripts/spec.js (in the same format). Try changing that and see if anything changes.

UPDATE:

Based on the comment thread below, it seems the code is getting to the this.project.save(...) line but the validations aren't working: I know this because if you're getting a POST error in the console, it means that backbone actually made the request (which it should not have because the name is empty). So you should go back and check the code you are actually testing.

4

3 に答える 3

6

I faced this problem when I downloaded sinon.js file from GitHub (without Sinon folder). I solved the problem by downloading the library from http://sinonjs.org/

于 2013-04-25T08:23:08.197 に答える
2

上記のコメントスレッドに基づいて、これを回答として投稿します。問題を が呼び出される行に絞り込んだsinon.spy()ので、このテストに固有のものではなく、sinon がどのように読み込まれているかに関係しています。

問題は、実際には spec/javascripts/spec.js (同じ形式) に入れる必要があるのに、application.js に sinon と jasmine-sinon を含めていることだと思います。それを変更してみて、何かが変わるかどうかを確認してください。

アップデート:

以下のコメント スレッドに基づいて、コードはthis.project.save(...)行に到達しているようですが、検証は機能していません。名前が空であるため、指定する必要はありません)。したがって、実際にテストしているコードに戻って確認する必要があります。

于 2012-08-26T01:00:00.700 に答える
0

I know this thread is old, but I had a similar issue today with this when going through this tutorial http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html. It looks like Backbone made a change and it calls the 'invalid' event when invalid model data is provided, not 'error'.

If you run into this error try changing:

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

To:

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

This resolved the issue for me.

于 2014-04-17T21:29:40.707 に答える