我对 Rails 比较陌生,我不确定为什么这个 rspec 测试失败了。
模型类
class Invitation < ActiveRecord::Base
belongs_to :sender, :class_name => "User"
before_create :generate_token
private
def generate_token
self.token = Digest::SHA1.hexdigest([Time.now, rand].join)
end
end
测试
it "should create a hash for the token" do
invitation = Invitation.new
Digest::SHA1.stub(:hexdigest).and_return("some random hash")
invitation.token.should == "some random hash"
end
错误:
Failure/Error: invitation.token.should == "some random hash"
expected: "some random hash"
got: nil (using ==)
邀请模型有一个 token:string 属性。有任何想法吗?谢谢!