You cannot always use spyware when testing, mainly for classroom expectations.
Example:
expect(User).to receive(:new)
There is no way to do this with a spy (unless you are injecting dependencies).
Now you can do the following:
user = double('user', save: true) expect(User).to receive(:new).and_return user User.new.save expect(user).to have_received(:save)
You clearly see that:
you must set expectations on a real object before running real code (this looks weird to set expectations before running code)
you can set the expectation of spies after real code, which is more natural
apneadiving
source share