看 Ruby on Rails Tutorial 过程中,一直照着书上的例子来,然后到 chapter 6 时,run rspec spec/models/user_spec.rb
时出现了如下错误:
Failures:
1) User when email address is already taken
Failure/Error: user_with_same_email.save
RuntimeError:
Password digest missing on new record
# ./spec/models/user_spec.rb:44:in `block (3 levels) in <t
op (required)>'
Finished in 0.09311 seconds
12 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:47 # User when email address i
s already taken
Randomized with seed 54648
user_spec.rb文件:
require 'spec_helper'
describe User do
#测试用例之前执行
before do
@user = User.new(
name: "xautjzd",
email: "[email protected]",
password: "xautjzd",
password_confirmation: "xautjzd"
)
end
#设置测试用例默认的测试对象
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should be_valid }
describe "when name was not present" do
before { @user.name = " " }
it { should_not be_valid }
end
describe "when email was not present" do
before { @user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.name = 'a' * 51 }
it { should_not be_valid }
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before do
@user = User.new(
name: "xautjzd",
email: "[email protected]",
password: " ",
password_confirmation: " "
)
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { @user.password_confirmation = "mismatch"}
it { should_not be_valid }
end
end
已通过rails g migration add_password_digest_to_users password_digest:string
添加此 column,也已经rake db:migrate
生成了,貌似是 before 中没有给 password_digest 字段赋值,我添加上就能通过测试,但是教程上并没有给此字段赋值,我参考了其他人学习此教材的例子,也没有赋值,但是都能通过测试。请问这是什么原因呢?