admin管理员组

文章数量:1315807

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:

.

Take care to use that particular mit.

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./ErikEvenson/spa-testing-study/tree/bcc5b71b3b6f8b24f7e8d01673b50682498ee1b2.

Take care to use that particular mit.

Share Improve this question edited Aug 22, 2013 at 21:50 Erik asked Aug 22, 2013 at 21:40 ErikErik 7,7518 gold badges64 silver badges100 bronze badges 6
  • Have you checked that sinon is what you expect it to be? – mu is too short Commented Aug 23, 2013 at 0:16
  • I did and it is not. It is an object, created by sinon.js, but it does not have a spy() method. – Erik Commented Aug 23, 2013 at 0:38
  • I'd have a look at the require.js set up then. I'm just throwing a few ideas at the wall to see what sticks; sorry I can't be more specific, I'm not that familiar with these tools. – mu is too short Commented Aug 23, 2013 at 1:07
  • I think you are right -- I suspect that sinon is not able to find its the directory that spy.js is in -- but I'm not sure how to remedy that... – Erik Commented Aug 23, 2013 at 2:00
  • Is there perhaps a sinon.sinon.spy? – mu is too short Commented Aug 23, 2013 at 2:26
 |  Show 1 more ment

2 Answers 2

Reset to default 7

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.

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./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.

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/releases/sinon-1.12.2.js#*",
 },
[...]

本文标签: javascriptSinon cannot find method 39spy39Stack Overflow