Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add propertyStub #161

Closed
shairez opened this issue Jul 16, 2012 · 8 comments
Closed

Add propertyStub #161

shairez opened this issue Jul 16, 2012 · 8 comments

Comments

@shairez
Copy link

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
Copy link
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
Copy link
Member

mantoni commented Jul 17, 2012

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

@cjohansen
Copy link
Contributor

Like so:

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

@avand
Copy link

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
Copy link
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
Copy link

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
Copy link
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.

@rconnamacher
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants