There are already questions , but they all override the return values to nilif not called.and_return
PROBLEM
I am wondering if there is a way to simply check if a method is called with expect_any_instance_of(Object).to receive(:somemethod), and it works fine, without overriding or affecting the return value .somemethod.
Consider the following:
it 'gets associated user' do
expect_any_instance_of(Post).to receive(:get_associated_user)
Manager.run_processes
end
class Manager
def self.run_processes
associated_user = Category.first.posts.first.get_associated_user
associated_user.destroy!
end
end
The spectrum above, although it will work because it :get_associated_useris called in run_processes, however, it raises NoMethodError: undefined method 'destroy!' for NilClassprecisely because I made fun of :get_associated_userany Post instance.
.and_return, expect_any_instance_of(Post).to receive(:get_associated_user).and_return(User.first), , ( ), , .
.and_return(correct_user), correct_user - , , . , , Category.first.posts.first.get_associated_user , . , , .