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
Argument Aliasing #687
Argument Aliasing #687
Conversation
@@ -901,6 +901,72 @@ if (typeof require === "function" && typeof module === "object") { | |||
} | |||
}, | |||
|
|||
".argNames": { | |||
//jscs:disable disallowDanglingUnderscores |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disable the "dangling underscore" lint rule for the just these tests. Otherwise this lint rule actually helps ensure that aliased arguments will never collide with existing api properties.
Thanks for taking the time to contribute to Sinon. However, I'd advise to discuss new features before spending time implementing them. I'm not so sure about this, it seems to clutter things up a little. E.g. If we were to support this, I'd prefer something like this: var spy = sinon.spy(function (path, callback) {});
spy("a/b", fn1);
spy("b/c", fn2);
assert.equals(spy.getCall(0).args.path, ["a/b", "b/c"]);
assert.same(spy.firstCall.args.callback, fn1);
spy.withArgs("b/c").firsCall.args.callback(); //calls fn2 It's not as concise, but there's also no magic, and properties are found where I'd expect them (e.g. arguments are accessed through |
I agree and had that thought after I was already typing up the PR.
Me either, but without it you run into naming collisions. Not a huge deal since sinon builds arrays using The idea of introspecting on the function to find arg names was one I already had, and I like it. But I didn't want to go down that road until I had feedback on the basic idea.
The initial code / test really took no time at all, so I wasn't worried about that (and won't be crushed if this never gets merged). Had I known the rabbit hole your linter was going to send me down, I definitely would have done things differently. Lesson learned. |
If we do decide to implement such a feature, I too would prefer to have regular names on the However, I cannot say that this is a feature that I've missed and I am not sure that it makes the resulting code that much clearer. If you're in a situation where you need this, you're probably better off simplifying your code... |
closing due to lack of interest |
Aliases arguments so you can access them via meaningful property names instead of by array indexes.
Configure argument aliases, probably best used in
setUp
orbeforeEach
(setup is bit verbose for a single test).Call the spy as you normally wood:
The
spy
object will now have custom-named properties, which are arrays populated with the named argument from each call.spyCall
objects also get custom-named properties, they aren't (necessarily) arrays, but rather the actual named argument passed to that particular call.The "
_
" prefix is added to avoid collisions with existing spy properties.There are at few real benefits to using this approach:
firstCall._path
conveys a bit more meaning thanfirstCall.args[0]
(though perhaps it would be a bit clearer if I added the properties to the array instead - i.e.firstCall.args._path
).._path
on your tests will likely return far fewer results than.args[0]
.setUp
, no need to refactor every test.