Ruby on rails 如何为self.method创建rspec测试?

Ruby on rails 如何为self.method创建rspec测试?,ruby-on-rails,rspec,Ruby On Rails,Rspec,我目前在我的用户类中有此方法: def self.authenticate(email, password) user = User.find_by_email(email) (user && user.has_password?(password)) ? user : nil end 我如何在这个平台上运行rspec测试 我试图运行it{responses_to(:authenticate)},但我假设self与authenticate不同 我仍然是rails的初学者

我目前在我的
用户
类中有此方法:

def self.authenticate(email, password)
  user = User.find_by_email(email)
  (user && user.has_password?(password)) ? user : nil
end
我如何在这个平台上运行rspec测试

我试图运行
it{responses_to(:authenticate)}
,但我假设self与authenticate不同


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

@depa的答案是好的,但出于选择的考虑,并且因为我更喜欢较短的语法:

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
describe User do
  let(:user) { User.create(:email => email, :password => password) }

  describe "Authentication" do
    subject { User.authenticate(user.email, user.password) }

    context "Given an existing user" do
      let(:email) { "foo@bar.com" }
      context "With a correct password" do
        let(:password) { "foo" }
        it { should eq(user) }
      end
      context "With an incorrect password" do
        let(:password) { "bar" }
        it { should be_nil }
      end
    end
  end
end
除了我对sytax的偏好之外,我相信与其他样式相比,sytax有两大好处:

  • 它使覆盖某些值变得更容易(正如我在上面对
    password
    所做的那样)
  • 更重要的是,它突出显示了未经测试的内容,例如空白密码、不存在的用户等
这就是为什么对我来说,使用
context
subject
以及
let
的组合要远远优于通常的风格