我有我的Rails模型验证测试的问题。验证功能正常(拒绝格式不正确的电子邮件),但测试失败。模型验证失败FactoryGirl Rspec,验证似乎被忽略
这是我从我的user.rb模型
class User < ActiveRecord::Base
attr_accessible :first_name, :middle_name, :last_name, :email, :password, :address, :city, :state, :zip_code, :phone_home, :phone_cell, :phone_work, :status_id, :password_confirmation, :is_test, :parent_paramed_id, :company, :logo, :is_agent, :is_paramed, :additional_emails
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validate :validates_single_email_format
validates_presence_of :email
validates_uniqueness_of :email
def validates_single_email_format
User.validates_single_email_format(self.email)
end
def self.validates_single_email_format(email)
email =~ VALID_EMAIL_REGEX
end
重要信息,这是我的user_spec.rb
describe User do
context "validate email address on create" do
before { User.delete_all }
context "fails" do
let(:user) { FactoryGirl.build(:user, :email => 'blah') }
before { user.save; binding.pry }
specify { user.should_not be_valid }
end
context "succeeds" do
let(:user) { FactoryGirl.build(:user, :email => '[email protected]') }
before { user.save; binding.pry }
specify { user.should be_valid }
end
end
end
,因为它是一个结合执行这是本次测试的输出。在每次保存之后撬动以查看在保存被调用后用户对象的样子:
From: /project/spec/models/user_spec.rb @ line 96 :
91: context "validate email address on create" do
92: before { User.delete_all }
93:
94: context "fails" do
95: let(:user) { FactoryGirl.build(:user, :email => 'blah') }
=> 96: before { user.save; binding.pry }
97: specify { user.should_not be_valid }
98: end
99:
100: context "succeeds" do
101: let(:user) { FactoryGirl.build(:user, :email => '[email protected]') }
[1] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_4::Nested_1>)> user
=> #<User id: 178, first_name: "Martha", middle_name: nil, last_name: "Magdalena", email: "blah", additional_emails: nil, password_hash: "$2a$10$HF1t3cIwQiwj7PN/DkBgwOJw667FFixFB3srksZypHxR...", password_salt: "$2a$10$HF1t3cIwQiwj7PN/DkBgwO", password_reset_token: nil, password_reset_sent_at: nil, address: nil, city: nil, state: nil, zip_code: nil, phone_home: nil, phone_cell: nil, phone_work: nil, status_id: 1, is_test: false, parent_paramed_id: nil, company: nil, logo: nil, is_agent: true, is_paramed: false, created_at: "2013-04-20 18:13:02", updated_at: "2013-04-20 18:13:02">
[2] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_4::Nested_1>)> exit
F
From: /project/spec/models/user_spec.rb @ line 102 :
97: specify { user.should_not be_valid }
98: end
99:
100: context "succeeds" do
101: let(:user) { FactoryGirl.build(:user, :email => '[email protected]') }
=> 102: before { user.save; binding.pry }
103: specify { user.should be_valid }
104: end
105:
106: end
107:
[1] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_4::Nested_2>)> user
=> #<User id: 179, first_name: "Martha", middle_name: nil, last_name: "Magdalena", email: "[email protected]", additional_emails: nil, password_hash: "$2a$10$LEA28MGLIdLuk6lPEPGmH.lW3QwkR9KoXgbULPHJZa4v...", password_salt: "$2a$10$LEA28MGLIdLuk6lPEPGmH.", password_reset_token: nil, password_reset_sent_at: nil, address: nil, city: nil, state: nil, zip_code: nil, phone_home: nil, phone_cell: nil, phone_work: nil, status_id: 1, is_test: false, parent_paramed_id: nil, company: nil, logo: nil, is_agent: true, is_paramed: false, created_at: "2013-04-20 18:13:11", updated_at: "2013-04-20 18:13:11">
[2] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_4::Nested_2>)> exit
.
Failures:
1) User validate email address on create fails
Failure/Error: specify { user.should_not be_valid }
expected valid? to return false, got true
# ./spec/models/user_spec.rb:97:in `block (4 levels) in <top (required)>'
Finished in 16.9 seconds
2 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:97 # User validate email address on create fails
对于某些即使它认为电子邮件的值为“无效”,但失败的测试返回true。如果我在对象上运行验证方法,它会回应说没有匹配。但由于某种原因,“预计有效?”测试返回true。我不确定为什么会发生这种情况。
任何帮助将不胜感激。我可以在现有的问题中找到任何类似的问题。
编辑:删除路径中的计算机的具体信息
我第一次启动时确实有过你的代码。 我应该提到我们正在制作实例方法,以便我们可以以其他方式和我们使用电子邮件的其他地方使用该方法。我发布的解决方案(尽管在该状态下不正确)更加模块化,可以更广泛地使用。 感谢您加入这个! – Matt 2013-04-21 21:02:03