在Michael Hartl 编写的 Ruby on Rails 教程的第 6.x 章中,我无法让 Rspec 通过email is not present
检查。
以我有限的知识:
- User_spec 使用 before do 之后的代码创建一个测试用户。
- 然后根据属性检查此用户
user.rb
以确保其:presence
有效。 - 如果有效,则检查返回 true 或 false。
- 在下一个代码中,在 { @user.email = " " } 之前将电子邮件设置为空
- 然后说
it { should_not be_valid }
但是,它失败User when email is not present
错误。
/spec/models/User_spec.rb
require 'spec_helper'
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should be_valid }
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
end
spec/models/user.rb
class User < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true
end
失败:
1) 电子邮件不存在时的用户失败/错误:它 { should_not be_valid } 预期 # 无效 # ./spec/models/user_spec.rb:18:in `block (3 levels) in '
Finished in 0.03278 seconds 4 examples, 1 failure Failed examples: rspec ./spec/models/user_spec.rb:18 # User when email is not present