Sinon: Add propertyStub

Created on 16 Jul 2012  ·  8Comments  ·  Source: sinonjs/sinon

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

Most helpful comment

Like so:

sandbox.stub(obj, "prop", 42);

All 8 comments

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.

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

Like so:

sandbox.stub(obj, "prop", 42);

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.

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') 

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 [email protected] 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
https://github.com/sinonjs/sinon/issues/161#issuecomment-233119094, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAAvwPE2Z0M4qgwP1Sn5M5U3nWvRun8Hks5qWJFigaJpZM4AEdMq
.

@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.

A little late jumping in, but you can work around some use cases for stubbing location with history.replaceState(), which doesn't trigger any reloads. Just be sure to restore the correct URI after your tests are finished. So in your test, something like:

before: function() { _originalURI = location.href; },
after: function() { history.replaceState(null, "", _originalURI); }
testQuery: function() {
  history.replaceState(null, "", "?a=1&b=2");
  var parsed = myQueryParser();
  assert.deepEqual(parsed, {a: "1", b: "2");
}

There's no way to block code that directly sets properties on 'location', so if something does a location.href = "http://redirect-to-some-site.com/", it'll still redirect the browser.

Was this page helpful?
0 / 5 - 0 ratings