2013-04-20 110 views
0

我有我的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。我不確定爲什麼會發生這種情況。

任何幫助將不勝感激。我可以在現有的問題中找到任何類似的問題。

編輯:刪除路徑中的計算機的具體信息

回答

1

兩件事。您的validates_single_email_format方法不僅需要返回false,還必須呼叫errors.add(:email, "Email is in wrong format!")纔會產生一些效果。

valid?方法不只是運行驗證,這也增加了錯誤消息errorserrors是空到底,那麼該模型被認爲是有效的。

不管怎麼說,你可以使用:

validates_format_of :email, with: VALID_EMAIL_REGEX 
1

驗證的語法不正確。只用兩行試試這個正確的。

EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
validates :email, presence: true, uniqueness: true, format: {with: EMAIL_REGEX} 
+0

我第一次啓動時確實有過你的代碼。 我應該提到我們正在製作實例方法,以便我們可以以其他方式和我們使用電子郵件的其他地方使用該方法。我發佈的解決方案(儘管在該狀態下不正確)更加模塊化,可以更廣泛地使用。 感謝您加入這個! – Matt 2013-04-21 21:02:03