Mocha: How do you set up the instance method?

Suppose this ruby ​​code is:

class User
  def self.failed_login!(email)
    user = User.find_by_email(email)
    if user
      user.failed_login_count = user.failed_login_count + 1
      user.save
    end
  end
end

I want to write a test that checks that user.save is never called when an invalid email address is specified. For example:.

it "should not increment failed login count" do
   User.expects(:save).never()
   User.failed_login!("doesnotexist")
end

This test is currently passing, but it is also passing when I provide a valid email address.

How to set up wait with Mocha? (or any other mocking framework), so that it checks the method of saving any instance of the user that is never called?

(preferably without bypassing / bullying the find_by_email method, since the implementation of how to force the user to change in the future)

Greetings

+3
source share
2

, , , RR, ... Mocha :

User.any_instance.expects(:save).never()
+16

-

user = mock
User.expects(:find).returns user
user.expects(:save).never
+4

Source: https://habr.com/ru/post/1709155/


All Articles