Search code examples
unit-testingbackbone.jsjasminesinon

Attempted to wrap sendRequest which is already wrapped


I'm writing unit tests using Jasmine for my Backbone cache and am trying to mock up a function response using Sinon.js. For different tests, I expect different things to happen so I am creating a mock before each test and deleting it after, then filling in the expects behavior within the test itself. However, I'm getting an error and the tests are failing.

Here's my spec with just the relevant tests (other tests aren't using mock):

describe("mysite.core.Cache.XFooInfo", function() {
    var fnMock;

    beforeEach(function() {
        fnMock = sinon.mock(fn);
    });

    afterEach(function() {
        delete fnMock;
    });

    it("should make a request after function fooCreated called", function() {
      fnMock.expects("sendRequest").once().withExactArgs("ModuleFoo", "getFoo", ["1000"]);

      events.trigger("fooCreated", [{Args:[test.data.XFooInfo]}]);
      fnMock.verify();
    });
 });
describe("mysite.core.Cache.XFooBarInfo", function() {
    var fnMock;

    beforeEach(function() {
        fnMock = sinon.mock(fn);
    });

    afterEach(function() {
        delete fnMock;
    });

    it("should make a request after function booUpdated called", function() {
      var booCopy = $.extend(true, {}, test.data.XBooInfo);

      booCopy[0].Args[0].FooID = "12345";
      fnMock.expects("sendRequest").once().withExactArgs("ModuleFoo", "getFoo", ["12345"]);

      events.trigger("booUpdated", booCopy);
      fnMock.verify();
    });
 });

The first test works fine and passes. The second test, however, gives me this error:

TypeError: Attempted to wrap sendRequest which is already wrapped
    at Object.wrapMethod (https://localhost:8443/mysite/web/tests/libs/sinon-1.7.1.js:528:23)
    at Object.expects (https://localhost:8443/mysite/web/tests/libs/sinon-1.7.1.js:2092:27)
    at null.<anonymous> (https://localhost:8443/mysite/web/shasta-cList-tests/spec/CacheSpec.js:909:15)
    at jasmine.Block.execute (https://localhost:8443/mysite/web/tests/libs/jasmine-1.2.0.rc3/jasmine.js:1024:15)
    at jasmine.Queue.next_ (https://localhost:8443/mysite/web/tests/libs/jasmine-1.2.0.rc3/jasmine.js:2025:31)
    at jasmine.Queue.start (https://localhost:8443/mysite/web/tests/libs/jasmine-1.2.0.rc3/jasmine.js:1978:8)
    at jasmine.Spec.execute (https://localhost:8443/mysite/web/tests/libs/jasmine-1.2.0.rc3/jasmine.js:2305:14)
    at jasmine.Queue.next_ (https://localhost:8443/mysite/web/tests/libs/jasmine-1.2.0.rc3/jasmine.js:2025:31)
    at jasmine.Queue.start (https://localhost:8443/mysite/web/tests/libs/jasmine-1.2.0.rc3/jasmine.js:1978:8)
    at jasmine.Suite.execute (https://localhost:8443/mysite/web/tests/libs/jasmine-1.2.0.rc3/jasmine.js:2450:14)

I can't find anything in the Sinon.js docs to tell me what I'm doing wrong. I know verify also does a restore on all the functions it mocks and I thought that it was enough to allow me to write a new expects behavior for the same function, but apparently I was wrong.

What is the right way to do this?


Solution

  • Ok, so after spending 2 hours on this problem last Friday with no luck, I figured it out twenty min after posting this question. Adding var fn.sendRequest = function(module, fnName, args) {}; to the beforeEach function fixed all the failing tests.