-
-
Notifications
You must be signed in to change notification settings - Fork 277
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
Cop Idea: Force to use "allow to receive" with "with" to match arguments #1217
Comments
What if This kind of goes against the recommendation from Effective testing with RSpec book to be as loose with expectations as possible. And this is not even an expectation, it's an allowance. Do you have a better example in mind? What is the rationale behind this proposal? |
if no arguments then |
Example: Code in controller:
Spec:
Result: Specs passed even if Job requires two arguments like |
I could recommend using Aside from this, why do you both |
We use https://github.com/que-rb/que
|
For the copIn this case, insisting that This boils down to a "test boundaries" recommendation. class MyJob < Que::Job
def run(credit_card_id, user_id)
...
expect(MyJob).to receive(:enqueue).with('4111 1111 1111 1111', user.id) # boom, job expects not a card number, but the card record id but the spec would pass. There's one more thing to keep in mind. expect(Job).to receive(:enqueue).twice.with(...)
# ^^^ Add a `with` expectation constraint for the second call to `enqueue` I can't remember off the top of my head how RSpec would constrain the arguments of a second call, would it use the same constraints from Do you want to shave this yak? For your specific caseQue's testing doc section states:
Is There's also |
It was not enough in our case. |
Would adding |
for |
Would adding |
Ok, let's start from the very beginning. class A
def self.three_args(a, b, c)
end
end
RSpec.describe A do
context 'when allow has no `with` qualifier' do
before { allow(A).to receive(:three_args) }
it 'fails to detect the problem with an incorrect number of arguments passed to the method call' do
expect(A).to receive(:three_args).with(1, 2)
A.three_args(1, 2)
end
end
end results in:
I believe this is due to config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end I highly recommend turning it on. It will be turned on by default in RSpec 4. Full example with class A
def self.three_args(a, b, c)
end
end
RSpec.describe A do
context 'when allow has no `with` qualifier' do
before do
allow(A).to receive(:three_args)
end
it 'fails to detect the problem with an incorrect number of arguments passed to the method call' do
expect(A).to receive(:three_args).with(1, 2)
A.three_args(1, 2)
end
end
context 'when allow has a comparably incorrect `with` qualifier' do
before do
allow(A).to receive(:three_args)
.with(1, 2) # Here it is what the cop would recommend adding. And it does not solve the problem
end
it 'fails to detect the problem with an incorrect number of arguments passed to the method call' do
expect(A).to receive(:three_args).with(1, 2)
A.three_args(1, 2)
end
end
end
💥 cop is useless. Use |
I'm not convinced this will make a useful cop, at least for what it was originally purposed to inspect. |
Example:
Good:
allow(Job).to receive(:enqueue).with(a, b, c)
Bad:
allow(Job).to receive(:enqueue)
to avoid scenarios when tests passed and there's incorrect number of arguments on production :D
The text was updated successfully, but these errors were encountered: