遵循 Michael Hartl rails 教程。努力让记忆令牌测试通过,特别是记忆令牌的非空白测试。
User 类的代码如下
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
before_save { :create_remember_token }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: {case_sensitive: false}
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
和我无法通过的测试:
subject { @user }
...
describe "remember token" do
before { @user.save }
its (:remember_token) { should_not be_blank }
end
我得到的错误信息是:
.....................F
Failures:
1) User remember token remember_token
Failure/Error: its (:remember_token) { should_not be_blank }
expected blank? to return false, got true
# ./spec/models/user_spec.rb:120:in `block (3 levels) in <top (required)>'
Finished in 0.68878 seconds
22 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:120 # User remember token remember_token
我不知道这是否相关,但 sublime_text 没有对 self 关键字做任何事情(即它没有以不同的颜色突出显示它)。
我正在使用 Ruby 1.9.3