Conditionally stubbing with a function

332 views
Skip to first unread message

Jacob Page

unread,
Mar 20, 2013, 4:24:24 PM3/20/13
to sin...@googlegroups.com
I'd like to create a conditional stub that invokes a function when called, but it appears that this feature doesn't exist. For example:

var jQuery = sinon.stub();
jQuery.invokes(function(selection) { return new JQuerySelection(selection); });
jQuery.withArgs('.foo').returns(fooObj);

I see that I can stub with a function using sinon.stub(object, 'method', function() { ... }), but this actually returns a spy instead of a stub instance. So this doesn't work, for example:

var dummy = { jQuery: function() { } };
var jQuery = sinon.stub(dummy, 'jQuery', function(selection) { return new JQuerySelection(selection); });
jQuery.withArgs('.foo').returns(fooObj); // Error... no returns method

Am I missing something in the API, or is this just not possible with Sinon?


--
Jacob

Christian Johansen

unread,
Mar 20, 2013, 4:47:23 PM3/20/13
to sin...@googlegroups.com
Not possible, and should mostly not be necessary either. Your options are:

1) Simplifying your tests to not cover so many uses in one go
2) Express the desired behavior in terms of withArgs, returns/yields etc
3) Use sinon.stub(obj, meth, fn) to provide a custom function

Christian
Reply all
Reply to author
Forward
0 new messages