-
-
Notifications
You must be signed in to change notification settings - Fork 357
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
Feature for specific argument constraints (ex/ double.stub(:foo).with_fourth_argument(...)) #429
Conversation
i.e. mock.stub(:method).with_second_argument(1)
b.argument_list_matcher.expected_args.count | ||
end | ||
|
||
max.argument_list_matcher.expected_args.count |
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.
If the original method is a stub, then the only way to figure out the number of arguments is to look at all of the stubs and take the max argument count. It doesn't feel like the cleanest solution, but there's really no other way to determine the number of arguments.
Thanks for the PR. This can also be solved by passing an implementation block: expect(x).to receive(:bar) do |*args|
expect(args[4]).to eq(value)
end Any reason that doesn't meet your needs? |
I didn't think about that syntax. Thanks for pointing that out! And thanks for the immediate reply! It solves the problem of having to update tests, but it's a bit more verbose compared to I'd like to hear your thoughts on the negatives of having the with_x_argument code/syntax? I realize I may be bias towards it since I've been thinking about it for a while :) |
I'm on the fence about it. Things that concern me are:
|
All very good arguments that are easy to agree with. Thanks for taking the time to share your thoughts! |
FWIW I agree with @myronmarston on this one. Thanks for the suggestion though! |
I'd have been open to a |
@JonRowe, the I do care about all of the parameters. It's just that there are some scenarios in which I prefer to address each parameter in a separate test. As for the tests breaking after adding a parameter, @myronmarston's argument was very convincing. If the code in your tests are hard to change, then the implementation in your actual code will also be hard to change, and the tests are telling you that about your design. Also, I've always limited myself to 5 arguments in a method, and I probably should limit myself to less than that. Limiting arguments is good practice, and in doing so, it further makes the proposed feature unnecessary. The last thing that I would want is a feature that promotes poor design, and I think this feature would so just that. This conversation reinforced a lot of good design practices with me. Thanks everyone for the feedback! |
That's generally an indication of a code smell; Test pain is a feedback mechanism to push your design :) |
Sometimes the method that you're calling and passing arguments to is a third party's API, and you don't have control of the design. |
I wanted to propose a new feature for rspec that allows an argument constraint style for a specific argument.
So, instead of:
I'd like it to be:
Some of the benefits
This pull request includes a working implementation that's tested, but I don't think it's all the way there. Before putting more time into the implementation, I first wanted to get some feedback from the rspec team to see if you would accept a feature like this. If the team does like it, I would love to get it to the point that meets the rspec team's standards, and also would love to hear suggestions regarding any implementation ideas.
Thanks!
Mike