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
Spy Restore() #236
Comments
Given that you can already: my.spy.andCallThrough(); is there any need for this function? It's true we'd have some problems in the case where spying on a method that has properties (such as a constructor), but we already have this issue. |
I didn't think about using |
I think this is still valuable. In some cases we want to fully remove to spy and restore the constructor. Method andCallThrough() is insufficient for this. |
That's a reasonable point. Probably deserves reconsideration. Reopening. |
I also vote for this, I am spying on several internal JS methods (like |
+1 |
+1, beforeEach(function () {
this._originalMethod = obj.prototype.method;
spyOn(obj.prototype, 'method');
});
afterEach(function () {
obj.prototype.method = this._originalMethod;
}); having a restore method would be cleaner: beforeEach(function () {
spyOn(obj.prototype, 'method');
});
afterEach(function () {
obj.prototype.method.restore();
}); |
+1 |
2 similar comments
+1 |
+1 |
I've added this story to our backlog. This is likely going to make it into 2.0. Closing. |
+1 |
1 similar comment
+1 |
+1 |
2 similar comments
+1 |
+1 |
To clarify, Jasmine 1.3.1 and Jasmine 2.0 restore the original function after that spec has completed. Thus from the above example: beforeEach(function () {
this._originalMethod = obj.prototype.method;
spyOn(obj.prototype, 'method');
});
afterEach(function () {
obj.prototype.method = this._originalMethod;
}); is not actually necessary -- the Additionally, in 2.0 we now preserve properties from the original function and move them over to the spy. This preservation + andCallThrough should hopefully make spies less obtrusive. There might still be some use cases that need the spy completely gone in the middle of a spec, but just wanted to point out that cleanup is taken care of. |
+1 |
2 similar comments
+1 |
+1 |
Does sheelc's comment also apply to jasmine_node? |
Jasmine has restored the spy's original value after the spec for a while now, so whatever version of jasmine has been bundled into jasmine-node should do this already. |
+1, I would like this in case I want to re-spy a method during the same run. I.e. the first time I want the function to do thing X, and later in that run I want it to do Y. |
@dts you can already do this by specifying a new plan. e.g. (warning untested code) it('spies a lot', function() {
var spy = jasmine.createSpy('spy).and.return('foo');
expect(spy()).toEqual('foo');
spy.and.callFake(function() {
return 'bar';
});
expect(spy()).toEqual('bar');
}); |
well, I guess I can simply use sinon if I really want this feature. sinon supports frontend.. |
@slackersoft - Yup - you're totally right! Consider my +1 rescinded! |
@guy-mograbi-at-gigaspaces (or anyone else) we're still not totally sure what is being asked for here. The issues I've seen are (I think):
If there's another issue I've missed or if |
Since I haven't seen anyone say I missed anything in my previous comment, it sounds like all of the cases where people were wanting to restore a spy are handled by jasmine currently. I'm going to close this issue. If there is another case I missed, please open an issue about that particular case. Thanks for using jasmine! |
+1 |
@tiriana as has been discussed in this issue, we don't believe it should ever be necessary for a user to manually restore a spy. If you have an actual suggestion of where this is needed, please create a new issue. |
@slackersoft I didn's see, sorry for too-fast '+1'. So jasmine already clears/restores spy after each spec? didn't know, but if so - that's cool, and enough for me. |
I'd like to +1 this too. It is for, I admit, a pretty obscure case. (I can change it so that it looks at spy.originalValue and notices that THAT is its own, so it knows it can restore Put more tersely: I guess, more generally, the point is: if for some reason one cares about the exact instance, and there is no way to tell jasmine 'I changed this behind your back', sad things happen (my workaround is to replace spy.originalValue with the value that I have already set $.ajax with. ugly? yes. I'm open to other ideas) |
@bodawei it sounds to me like you probably want to be managing your testing around |
Thanks for the suggestion, @slackersoft. Alas, that really isn't an option. We've got a mock server ( https://github.com/delphix/dxData/tree/master/dxCoreData/mockServer ) which sits in the background behind all our tests, and automagically services all server interactions. It kinda does the same thing as jasmine-ajax but more automatically. Since it is so omnipresent in our tests, turning it on and off in some fashion to get around the jasmine situation is much too cumbersome (since spying on $.ajax is a distinct minority of our test cases). I can see an alternative would be to have the global beforeEach install a spy at |
You might also be able to: spyOn($, 'ajax').and.callThrough(); ... in a global |
I agree, that is another possibility. |
How can I check |
@guy-mograbi-at-gigaspaces, I for things like that, I use the call count (.e.g mySpy.calls.count()) to see if the number of counts has changed between calls. |
A restore() method would be useful in my use case. reset() and callThrough() don't need to do what I need. Use Case:
The only way I can see to do this now is by mocking every method but the one I want and create the instance in every single test. However that means I'm testing that the constructor calls the method AND the method itself, which I don't want. Unless there is a better way to architect this type of code? .reset() doesn't work because it seems to only reset the number of calls |
Why doesn't this exist yet? Use case 1: in a more "global" beforeEach, you set up a spy. Then, in a very specific test, you need to override that spy with another spy. Overriding with |
@brandonros exactly same case here
Pushing the definition down to them would mean adding 3 repetitive lines per group. |
If you simply need to change the behavior of the spy when called, you should be able to use another @brandonros can you go into more depth about why |
This idea comes from
sinon.js
where you can setup a spy then later on restore it back to the original method in the case you no longer need or want the spy.This works well when having a spy setup that is used in most cases but in the rare case it isn't, it's more useful to set one up then for the special case remove it.
Note: this does not clean up after itself for
removeAllSpies
but doesn't have any bad side effects.The text was updated successfully, but these errors were encountered: