以下用戶測試通過,沒有問題,用戶是有效的: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
不工作?
謝謝,我已經添加了'@persona.user = @ user',但我現在(感謝'@ persona.errors.full_messages')得到'User can not be blank'的錯誤。爲什麼它認爲用戶是空白的? – Bazley
@Bazley改爲使用'@persona.user_id = @ user.id'。這應該工作! –