2014-12-06 92 views
1
def self.find_for_database_authentication(warden_conditions) 
    conditions = warden_conditions.dup 
    if login = conditions.delete(:login) 
     where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first 
    else 
     where(conditions).first 
    end 
    end 

以上是验证方法,代码来自Model。如何编写用于设计验证方法的Rspec测试

我无法理解如何在此测试方法中将warden_conditions作为参数传递。

你能帮助我如何通过warden_condition作为参数Rspec(单位)Testiong?

感谢

回答

2

这似乎是一个类的方法,并warden_conditions似乎只是一个哈希,所以你可以使用这样的事情

let(:email) { "[email protected]" } 
let(:warden_conditions) { { login: email } } 

it "finds user by email" do 
    user = User.create(email: email) 
    authenticated = User.find_for_database_authentication(opts) 
    expect(authenticated).to eql user 
end 

it "finds user by username" do 
    user = User.create(username: email) 
    authenticated = User.find_for_database_authentication(opts) 
    expect(authenticated).to eql user 
end