Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinon cannot find method 'spy'

I am trying to climb the learning curve on using require.js/mocha/chai/sinon with backbone apps. When I run this test:

define([
    "chai",
    "sinon"
], function(chai, sinon){
    var expect = chai.expect;

    describe("Trying out the test libraries", function(){
        describe("Chai", function(){
            it("should be equal using 'expect'", function(){
                expect(hello()).to.equal("Hello World");
            });
        });

        describe("Sinon.JS", function(){
            it("should report spy called", function(){
                var helloSpy = sinon.spy(window, "hello");

                expect(helloSpy.called).to.be.false;
                hello();
                expect(helloSpy.called).to.be.true;
                hello.restore();
            });
        });
    });
});

I get TypeError: Object #<Object> has no method 'spy' on the line where helloSpy is defined. Why? Note that the first test passes.

Here is the full project:

https://github.com/ErikEvenson/spa-testing-study/tree/bcc5b71b3b6f8b24f7e8d01673b50682498ee1b2.

Take care to use that particular commit.

like image 947
Erik Avatar asked Aug 22 '13 21:08

Erik


2 Answers

The problem here turns out to be that the bower repository for sinon is unusable as is per this issue. Sinon has to be built first and doing bower install sinon just pulls the Sinon.JS repo down. Using bower install sinonjs instead of bower install sinon works, but gives an earlier version number.

like image 52
Erik Avatar answered Nov 09 '22 13:11

Erik


From @Erik link.

install --save-dev sinonjs-built

This will get you build version of sinon.

Edit

a another bower version ( as @Erik suggested above) may be found in https://github.com/blittle/sinon.js

one can install it via install --save-dev sinonjs

Edit 2

from sinon github:

Important: AMD needs pre-built version Sinon.JS as source doesn't work with AMD loaders (when they're asynchronous, like loading via script tags in the browser). For that you will have to use a pre-built version. You can either build it yourself or get a numbered version from http://sinonjs.org.

Solution: Tell bower the direct link of sinon file

You can edit the bower.json file. and instead of writing version just pass a url for the file i.e

[...]
"devDependencies": {
     "chai": "~1.10.0",
    "sinon": "http://sinonjs.org/releases/sinon-1.12.2.js#*",
 },
[...]
like image 30
oak Avatar answered Nov 09 '22 13:11

oak