Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Rubocop prefer `have_received` to `receive`?

I have tests of the form:

expect(ClassA).to receive(:method)
ClassB.perform

Rubocop would prefer if I refactored this to use have_received, which requires ClassA to be mocked. In other words, I need to set up:

allow(ClassA).to receive(:method)
ClassB.perform
expect(ClassA).not_to have_received(:method)

What's the point? Just following the Arrange Act Assert format?

like image 994
Will Taylor Avatar asked Oct 20 '25 14:10

Will Taylor


1 Answers

Refactoring to used have_received allowed me to move a lot of the set-up into a before block, and put tests after the action following the Arrange Ace Assert format.

The code noticeably reads better.

like image 185
Will Taylor Avatar answered Oct 23 '25 04:10

Will Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!