2014-12-21 166 views
0

以下用戶測試通過,沒有問題,用戶是有效的:Rails的單元測試失敗

user_test.rb:

require 'test_helper' 

class UserTest < ActiveSupport::TestCase 

    def setup 
    @user = User.new(name: "Example User", email: "[email protected]", callsign: "example", 
        password: "foobar", password_confirmation: "foobar") 
    end 

    test "user should be valid" do 
    assert @user.valid? 
    end 
end 

用戶模型:

class User < ActiveRecord::Base 

    attr_accessor :remember_token, :activation_token, :reset_token 

    has_many :personas, dependent: :destroy 
    has_secure_password 

    before_save do 
    email.downcase! 
    callsign.downcase! 
    end 
    before_create :create_activation_digest 

    validates :name, presence: true, 
        length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i 
    validates :email, presence: true, 
        format:  { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 
    VALID_CALLSIGN_REGEX = /\A[a-z\d\-.\_]+\z/i 
    validates :callsign, presence: true, 
         length:  { maximum: 20 }, 
         format:  { with: VALID_CALLSIGN_REGEX }, 
         uniqueness: { case_sensitive: false } 
    validates :password, length: { minimum: 6 }, allow_blank: true 

    def to_param 
    callsign 
    end 
    . 
    . 
end 

然而,當我在persona_test中設置完全相同的用戶,驗證失敗。 (人物角色驗證失敗太,每個用戶的has_many人物角色)

persona_test.rb:

require 'test_helper' 

class PersonaTest < ActiveSupport::TestCase 

    def setup 
    @user = User.new(name: "Example User", email: "[email protected]", callsign: "example", 
         password: "foobar", password_confirmation: "foobar") 
    @persona = @user.personas.build(name: "Bazman", callsign: "Baz") 
    end 

    test "user should be valid" do 
    assert @user.valid? 
    end 

    test "persona should be valid" do 
    assert @persona.valid? 
    end 
end 

假面模型:

class Persona < ActiveRecord::Base 

    belongs_to :user 

    before_save do 
    self.callsign.downcase! 
    set_persona_id 
    end 

    validates :name, presence: true, 
        length: { maximum: 50 } 
    VALID_CALLSIGN_REGEX = /\A[a-z\d\-.\_]+\z/i 
    validates :callsign, presence: true, 
         length:  { maximum: 20 }, 
         format:  { with: VALID_CALLSIGN_REGEX }, 
         uniqueness: { case_sensitive: false } 
    validates :user_id, presence: true 
    validates :persona_id, presence: true 

    def to_param 
    callsign 
    end 
    . 
    . 
end 

失敗測試輸出:

FAIL["test_user_should_be_valid", PersonaTest, 0.754914] 
test_user_should_be_valid#PersonaTest (0.75s) 
Failed assertion, no message given. 
test/models/persona_test.rb:18:in `block in <class:PersonaTest>' 

FAIL["test_persona_should_be_valid", PersonaTest, 0.893247] 
test_persona_should_be_valid#PersonaTest (0.89s) 
Failed assertion, no message given. 
test/models/persona_test.rb:22:in `block in <class:PersonaTest>' 

我不不明白爲什麼當設置用戶與th相同時,persona_test.rb中的用戶驗證失敗一個在user_test.rb中。你是否被允許在Personas測試中測試用戶?如果是這樣,我該如何成功測試角色?每個角色都屬於一個用戶,所以我必須創建一個用戶才能創建角色。

編輯:

persona_test.rb:

require 'test_helper' 

class PersonaTest < ActiveSupport::TestCase 

    def setup 
    @user = User.new(name: "Example User", email: "[email protected]", callsign: "example", 
         password: "foobar", password_confirmation: "foobar")#, activated: true) 
    @persona = @user.personas.build(name: "Bazman", callsign: "Baz") 
    @persona.user = @user 
    @persona.persona_id = 1 
    end 

    test "user should be valid" do 
    assert @user.valid?, @user.errors.full_messages 
    end 

    test "persona should be valid" do 
    assert @persona.valid?, @persona.errors.full_messages 
    end 
end 

通過上面的更新角色的測試,我得到錯誤信息 '用戶不能爲空'。爲什麼是

@persona.user = @user 

不工作?

回答

1

的原因失敗的斷言是,在Persona一些驗證不會通過:

validates :user_id, presence: true 
validates :persona_id, presence: true 

的驗證將它們保存到數據庫之前被運行。對於新記錄,user_idpersona_id仍然是nil

因爲Persona無效,所以User在其他測試中也是無效的。

1

在你的人物模型,你有:

validates :user_id, presence: true 
    validates :persona_id, presence: true 

但它看起來並不像一個user_id是被設置。嘗試在您的測試中使用@persona.user = @user進行設置。

此外,作爲調試工具,您可以在您的測試中打印@persona.errors.full_messages,以查看其未驗證的位置。

E.g. assert @persona.valid?, @persona.errors.full_messages

希望有所幫助。

編輯:根據下面的評論,該行實際上應該是@persona.user_id = @user.id。另一種可以達到相同效果的方法是將記錄實際保存到數據庫中。因此,在您的設置功能中,您將使用create而不是build。但是,這會變慢。

+0

謝謝,我已經添加了'@persona.user = @ user',但我現在(感謝'@ persona.errors.full_messages')得到'User can not be blank'的錯誤。爲什麼它認爲用戶是空白的? – Bazley

+0

@Bazley改爲使用'@persona.user_id = @ user.id'。這應該工作! –