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
Unsure how / no way to replace
methods with fakes
#1850
Comments
I ran your example with Are you still able to reproduce this issue? |
That's a very strange result, I'm testing at home now (still Linux) on Node 10 with both the REPL and a js file, and I'm still seeing |
runkit repro: https://runkit.com/akdor1154/5b415c4a4999cd0012bb094d |
Confirmed the issue on my own machine (Ubuntu 18.04, Node 8.10.0). The explicitly bound method workaround does seem to produce the expected result if used in a slightly different way:
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
still no way in 6.3.3. @mroderick I'm really confused as to how you managed to get |
I verified this too, just replacing
```
const assert = require("assert");
const sinon = require("sinon");
const method = function() {
return this.a;
};
const fakeMethod = sinon.fake(method);
const o = { sinon.replace(o, "replacedMethod", sinon.fake(o.replacedMethod)); assert.equal(o.method(), "asdf");
|
EDIT: I posted something wrong. Buggy code. Fixed now It doesn't seem to use the outer context in the wrapped function. const assert = require("assert");
const sinon = require("sinon");
const method = sinon.stub().callsFake(function() {
return this.foo;
});
const o = {
foo: "asdf",
method
};
//o.fakeMethod = sinon.fake(method.bind(o)); // works
o.fakeMethod = sinon.fake(method); // doesn't use `this` when invoked
const results = o.fakeMethod();
assert.equal(method.callCount, 1);
assert.equal(o.fakeMethod.callCount, 1);
assert.equal(results, "asdf"); |
OK, found a fix. It was the most obvious fix, but a bug in the driver code seemed to imply it was wrong. Submitting a PR. |
Published as 6.3.4 |
Describe the bug
If I fake a method (a function with a
this
reference) withsinon.fake
, it looks like itsthis
reference will become broken and unbindable.This makes fakes entirely useless for stubbing methods of OOP style code.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Setup as above.
Context (please complete the following information):
The text was updated successfully, but these errors were encountered: