Add propertyStub #161

Closed
shairez opened this Issue Jul 16, 2012 · 7 comments

Projects

None yet

5 participants

@shairez
shairez commented Jul 16, 2012

Currently, we have a method stub, so we can stub an object's method, and if that object doesn't have this method or its name changes, sinon throws an exception.

Some of my tests are depended on object's property, which I want to also stub and to have the same failure mechanism as methods stub have.

Why property stubs you ask? I use it to test things like bindings and such.

Is there a reason not to add it? do you have a proposal on how to implement it? (I don't mind creating the PR myself, just wanted to get a second opinion).

Thanks,
Shai

@cjohansen
Contributor

The reason this does not already exist is because stubbing a property is as easy as obj.prop = 42;. However, if I understand you correctly, you want that to fail (i.e. throw an exception) if obj does not already have a property prop?

You can do this today if you're working through a sandbox object. The reason property stubbing is available only in sandboxes, is that they can be automatically reverted, which provides some sort of convenience.

@mantoni
Member
mantoni commented Jul 17, 2012

Didn't know the is property stubbing for sandboxes! How can I use it?

@cjohansen
Contributor

Like so:

sandbox.stub(obj, "prop", 42);
@cjohansen cjohansen closed this Jan 4, 2014
@avand
avand commented Jul 15, 2016

I think this behavior would be useful at least in one particular case: stubbing window.location. Currently, I have no idea how to write tests for my query string utility methods. Anytime you do window.location = "?foo=bar" the page reloads and my tests stop running. I guess I could put those particular tests on a different page.

@jonnyreeves
Contributor

Hi @avand, built in browser globals, like window.location can not be stubbed by sinon, instead we recommend you wrap such methods in your own api which you can then stub using sinon, eg:

var urlUtils = {} 
urlUtils.setLocation = function (value) {
  window.location = value 
} 

// in your test code 
sinon.stub(urlUtils, 'setLocation') 
@avand
avand commented Jul 18, 2016

Is it impossible for Sinon to stub an object's properties? Or is the issue
with stubbing globals? I'm just curious to know more about why Sinon
doesn't support stubbing properties. It's a feature I would expect in a
stubbing framework.
On Sat, Jul 16, 2016 at 01:08 John Reeves notifications@github.com wrote:

Hi @avand https://github.com/avand, built in browser globals, like
window.location can not be stubbed by sinon, instead we recommend you
wrap such methods in your own api which you can then stub using sinon, eg:

var urlUtils = {} urlUtils.setLocation = function (value) {
window.location = value
}
// in your test code sinon.stub(urlUtils, 'setLocation')


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#161 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAAvwPE2Z0M4qgwP1Sn5M5U3nWvRun8Hks5qWJFigaJpZM4AEdMq
.

@mantoni
Member
mantoni commented Jul 18, 2016

@avand Sinon can stub properties (using the sandbox feature). It can also stub globals, if they are user defined. Sinon can not stub some special browser built-ins, for example the location object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment