最后我闯红灯...
用户.rb
class User < ActiveRecord::Base
has_secure_password
validates :password, :presence => { :on => :create }
end
工厂.rb
Factory.define :user do |f|
f.email "foo@bar.com"
f.password "foobar"
f.password_confirmation { |u| u.password }
end
spec_user.rb
describe User do
it "should authenticate with matching username and password" do
user = Factory(:user, :email => 'frank@gmail.com', :password => 'secret')
User.authenticate('frank@gmail.com', 'secret').should == user
end
end
我得到一个红灯...
Failure/Error: user = Factory(:user, :email => 'frank@gmail.com', :password => 'secret')
NoMethodError:
undefined method `password_digest=' for #<User:0xb383460>
我认为这是 rake db:migrate 问题,我查看了 rails c ,但显然 password_digest 已定义。
ruby-1.9.2-p180 :007 > a = User.new
=> #<User id: nil, email: nil, password_digest: nil, is_admin: nil, created_at: nil, updated_at: nil>
ruby-1.9.2-p180 :008 > a.password_digest = 3
=> 3