简体   繁体   English

Rails单元测试失败

[英]Rails unit test failing

The following User test passes with no problem, the user is valid: 以下用户测试顺利通过,用户有效:

user_test.rb: user_test.rb:

require 'test_helper'

class UserTest < ActiveSupport::TestCase

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

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

User model: 用户模型:

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+\-.]+@[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

However, when I set up exactly the same user in the persona_test, the validation fails. 但是,当我在persona_test中设置完全相同的用户时,验证将失败。 (The persona validation fails too, each User has_many personas) (角色验证也失败,每个用户都有多个角色)

persona_test.rb: persona_test.rb:

require 'test_helper'

class PersonaTest < ActiveSupport::TestCase

  def setup
    @user = User.new(name: "Example User", email: "user@example.com", 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

Persona model: 角色模型:

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

Failed test output: 测试输出失败:

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>'

I don't understand why the User validation in persona_test.rb is failing when the setup user is identical to the one in user_test.rb. 我不明白为什么当设置用户与user_test.rb中的用户相同时,persona_test.rb中的User验证失败。 Are you not allowed to test Users in a Personas test? 您是否不允许在角色测试中测试用户? If so, how do I successfully test personas? 如果是这样,我如何成功测试角色? Each persona belongs_to a user, so I have to create a user in order to create a persona. 每个角色都属于一个用户,因此我必须创建一个用户才能创建一个角色。

EDIT: 编辑:

persona_test.rb: persona_test.rb:

require 'test_helper'

class PersonaTest < ActiveSupport::TestCase

  def setup
    @user = User.new(name: "Example User", email: "user@example.com", 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

With the updated persona test above, I get the error message 'User can't be blank'. 通过上面更新的角色测试,我收到错误消息“用户不能为空”。 Why is 为什么是

@persona.user = @user

not working? 不工作吗?

In your persona model you have: 在角色模型中,您具有:

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

But it doesn't look like a user_id is being set. 但是看起来好像没有设置user_id。 Try setting it with @persona.user = @user in your test. 尝试在测试中使用@persona.user = @user进行设置。

Additionally, as a tool for debugging, you can print @persona.errors.full_messages in your test to see where exactly it is not validating. 另外,作为调试工具,您可以在测试中打印@persona.errors.full_messages ,以查看其确切位置尚未验证。

Eg assert @persona.valid?, @persona.errors.full_messages 例如assert @persona.valid?, @persona.errors.full_messages

Hope that helps. 希望能有所帮助。

EDIT : as per the comments below, the line should actually be @persona.user_id = @user.id . 编辑 :根据下面的注释,该行实际上应该是@persona.user_id = @user.id Another way you could achieve the same effect is to actually save the records to the database. 您可以实现相同效果的另一种方法是将记录实际保存到数据库中。 So in your setup function, you would use create instead of build . 因此,在设置功能中,应使用create而不是build This would, however, be slower. 但是,这会比较慢。

The reason for the failed assertion is that some validations in Persona won't pass: 断言失败的原因是在Persona中的某些验证不会通过:

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

The validations are run before saving them to the database. 在将验证保存到数据库之前,先运行验证。 For new records, user_id and persona_id will still be nil . 对于新记录, user_idpersona_id仍为nil

Because Persona is invalid, the User will be invalid in the other test as well. 由于Persona无效,因此User在其他测试中也将无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM