You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
However persisted? and save are useless because they are not class method. The spec would pass even if they return false.
allow(Contact).toreceive(:persisted?).and_return(false)# Not calledallow(Contact).toreceive(:order).with('lastname, firstname').and_return([contact])allow(Contact).toreceive(:find).with(contact.id.to_s).and_return(contact)allow(Contact).toreceive(:save).and_return(false)# Not called
Moreover, this example tests GET #show, so order method is not used either. After all, required stub is only this one:
I think beginners cannot distinguish these rules. They would believe all of them are required. So the code should be fixed to teach them valid stubbing.
P.S.
allow_any_instance_of(Contact).to receive(:save).and_return(true) is okay for Contact instance but RSpec does not recommend using allow_any_instance_of method:
contacts_controller_spec stubs like this:
However
persisted?
andsave
are useless because they are not class method. The spec would pass even if they return false.Moreover, this example tests GET #show, so
order
method is not used either. After all, required stub is only this one:I think beginners cannot distinguish these rules. They would believe all of them are required. So the code should be fixed to teach them valid stubbing.
P.S.
allow_any_instance_of(Contact).to receive(:save).and_return(true)
is okay for Contact instance but RSpec does not recommend usingallow_any_instance_of
method:https://www.relishapp.com/rspec/rspec-mocks/v/3-2/docs/working-with-legacy-code/any-instance
The text was updated successfully, but these errors were encountered: