您如何为 self.method 创建 rspec 测试?

Geo*_*ran 6 rspec ruby-on-rails

我目前在我的User班级中有这个方法:

def self.authenticate(email, password)
  user = User.find_by_email(email)
  (user && user.has_password?(password)) ? user : nil
end
Run Code Online (Sandbox Code Playgroud)

我如何对此运行 rspec 测试?

我试图运行it { responds_to(:authenticate) },但我认为 self 事物与身份验证不同。

我仍然是 Rails 的初学者,任何有关如何测试和解释self关键字的提示将不胜感激!

dep*_*epa 5

describe User do
  let(:user) { User.create(:email => "foo@bar.com", :password => "foo") }

  it "authenticates existing user" do
    User.authenticate(user.email, user.password).should eq(user)
  end

  it "does not authenticate user with wrong password" do
    User.authenticate(user.email, "bar").should be_nil
  end
end
Run Code Online (Sandbox Code Playgroud)