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
Named spies #250
Comments
Sinon inspects the wrapped function name. So you can name the spy by wrapping an empty named function. var spy = sinon.spy(function doStuff() {}); This technique also allows you to produce a function with a specific length: var spy = sinon.spy(function doStuff(a, b, c) {}); // spy.length === 3 |
As far as I can see, it's always "proxy" as a function name. Are you sure this comment is true? |
Would you mind posting a runnable example? |
// test/index.js
var sinon = require('sinon');
var spy = sinon.spy(function doStuff() {
console.log('Name: ' + spy.name);
});
spy();
I can't remember what I did before, but I was getting Mocha output from failing tests where the function name was "proxy" and you wouldn't know it was "doStuff". If I can remember what exactly what it was, I will post |
I'm seeing this; it always says the name is It'd be great to reopen this. |
I got around this through the following: const mapMethod = sinon.spy(Array.prototype, 'map');
const methodName = map.wrappedMethod.name;
console.log(methodName); // 'map' |
@Tbhesswebber that doesn't work when non-test code is relying on the function's name. |
Ah... excellent point. I'm working on curricula for teaching higher-order functions (using P.S. - testing implementation details is really tedious when you're actually trying to! |
I can see how this would be useful. If anyone wants to dig deeper into this, that would be cool. It might be that there are features or tests that depend on the name of the returned function. If that's the case, we should discuss how to get around it. |
@mroderick when creating a spy with an original, |
I think this used to be a feature we had – naming the spy by giving the function a name – guess it got lost on the way. I’m fine with the proposal to allow naming it explicitly. But shouldn’t we also change the „proxy“ name? I feel like a spied function should look as close as possible like the original one. We ensure the correct arity, but we change the name now. Seems wrong. |
I just realized that we also have |
The patch from #1987 was released in |
It would help with readability of assertion error messages if spies (other than the object property ones) could be created with names. For example, Jasmine spies allow this.
The text was updated successfully, but these errors were encountered: