Question

Im testing some unit in my js code using Sinon.

And the question is - how can I change behavior of mocked object's method?

For example, i have some object, that have the reject method. I want him to return some calculated value, and expect, that it called once with proper context. I do the folowing:

var myObj = {reject: function(){
  // here original behavior
}};

sinon.stub(myObj, 'reject', function() {
  // here my test behavior
});

sinon.mock(myObj).expects('reject').once().on(someContext);

But finally im getting error: TypeError: Attempted to wrap reject which is already spied on

How can I solve my task?

Thanks!

Was it helpful?

Solution

It looks like you're trying to stub a method TWICE. You would want to add your expectations to the result of the first stub:

var myObj = {reject: function(){
    // here original behavior
}};

myObjStub = sinon.stub(myObj, 'reject', function() {
    // here my test behavior
});

myObjStub.once().on(someContext);

I think if you want to basically stub a custom behavior ('here my test behavior' and then ensure that it is called once you do:

var myObj = {reject: function(){
    // here CUSTOM behavior
}};

myObjStub = sinon.mock(myObj);
myObjStub.once().on(someContext);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top