Skip to content

Feature for specific argument constraints (ex/ double.stub(:foo).with_fourth_argument(...))

gitlab-qa-bot requested to merge github/fork/mikeabiezzi/master into master

Created by: mikeabiezzi

I wanted to propose a new feature for rspec that allows an argument constraint style for a specific argument.

So, instead of:

it "verifies one part" do
  ...
  x.should_receive(:foo).with(value, anything, anything, anything, anything)
  ...
end

it "verifies the other part" do
  ...
  x.should_receive(:foo).with(anything, value, anything, anything, anything)
  ...
end

it "verifies yet some other part" do
  ...
  x.should_receive(:foo).with(anything, anything, value, anything, anything)
  ...
end

I'd like it to be:

it "verifies one part" do
  ...
  x.should_receive(:foo).with_first_argument(value)
  ...
end

it "verifies the other part" do
  ...
  x.should_receive(:foo).with_second_argument(value)
  ...
end

it "verifies yet some other part" do
  ...
  x.should_receive(:foo).with_third_argument(value)
  ...
end

Some of the benefits

  • It's easier to read
  • Tests won't need to be updated if you add additional parameters to a method. With the current style of using anything, if I were to add a parameter to a method, I would then have to update all my with(...) constraints to include another anything argument. This feature eliminates the need to rework the existing tests.

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

Merge request reports