我在 Rails 中有一个 setter 方法,可以打印出姓氏(调试信息):
def password=(new_password)
logger.info 'DEBUG: self.lastname: ' + self.lastname
self.password_hash = SCrypt::Password.create(new_password, :key_len => 512, :salt_size => 32)
end
如果我使用控制台(开发或测试环境),我会得到:
2.0.0p0 :001 > u = User.new
=> #<User uuid: nil, account_id: nil, username_id: nil, password_hash: "", can_login: true, firstname: "", lastname: "", phone: "", mobile: "", fax: "", role: 0, created_at: nil, updated_at: nil>
2.0.0p0 :002 > u.lastname = "some lastname"
=> "some lastname"
2.0.0p0 :003 > u.password = "test"
DEBUG: self.lastname: some lastname
=> "test"
但是,当我运行 RSpec 测试并跟踪 log/test.log 和 grep 以进行调试时,我只是得到:
DEBUG: self.lastname:
好像在 RSpec 测试中无法访问姓氏(这是用户模型的 RSpec 测试)。它是 RSpec 中的错误还是我在这里做错了什么?
工厂定义为:
FactoryGirl.define do
factory :user do
account
current_username 'user@domain.com'
password 'iTho6chi'
can_login true
firstname 'John'
lastname 'Doe'
phone '555-1234'
mobile '555-4321'
fax '555-4321'
end
end
和:
require 'spec_helper'
describe User do
it 'should create a new user using the FactoryGirl registration of user' do
FactoryGirl.create(:user).should be_valid
end
end
给出:
Finished in 0.376 seconds
1 example, 0 failures
并且 test.log 文件给出:
DEBUG: self.lastname:
(姓氏在任何地方都没有设置,需要在模型中设置,所以在测试通过后设置)