Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinon: Stub and entire object and replace it with a new object

Using Sinon, how does one stub an entire object and all of its methods and then replace them with another object?

I have tried something like this, but it doesnt seem to work:

var stubby = sinon.stub(stubbedObj);
stubby.returns(newReplacementObj);

console.log(stubby); // this returns stub, but it would think that it should return newReplacementObj? 
like image 915
psvj Avatar asked Jan 31 '26 11:01

psvj


1 Answers

You could also consider refactoring your code to call a function that you can then stub instead. Example instead of stubbing out window.something, you can make an accessor method that uses window.something, and then stub out that accessor method.

like image 63
Brian R. Bondy Avatar answered Feb 03 '26 01:02

Brian R. Bondy