Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec for should_receive and should_not_receive both passed for Exception

I had a really weird rspec case scenario. I tried to test if my function handles exception correctly. And the following is my code:

in User.rb:

def welcome_user
      begin
        send_welcome_mail(self)
      rescue Exception => exception
         ErrorMessage.add(exception, user_id: self.id)
      end
    end
end

in user_spec.rb

it "adds to error message if an exception is thrown" do
  mock_user = User.new
  mock_user.stub(:send_welcome_mail).and_raise(Exception)
  ErrorMessage.should_receive(:add)
  mock_user.welcome_user
end

The test passed, but when I change ErrorMessage.should_receive(:add) to ErrorMessage.should_not_receive(:add), It also passed, any insights?

like image 895
Marshall Shen Avatar asked Jul 03 '12 22:07

Marshall Shen


2 Answers

Since rspec 2.11 exposed one of my tests to exhibit such "abnormality", I decided to raise the issue on github. You may following the discussion at https://github.com/rspec/rspec-mocks/issues/164

Summary: any_instance.should_not_receive is undefined, avoid

like image 157
prusswan Avatar answered Oct 30 '22 07:10

prusswan


What you can try to use instead is .should_receive in combination with .never:

ErrorMessage.should_receive(:add).never
like image 21
Rustam A. Gasanov Avatar answered Oct 30 '22 07:10

Rustam A. Gasanov