【问题标题】:This code is failing test in automated testing, but works in rails console此代码在自动化测试中未通过测试,但在 rails 控制台中有效
【发布时间】:2019-11-29 09:27:21
【问题描述】:

我是一名新的编码员,所以这可能很愚蠢。以下代码在我的测试套件中失败,但在 rails 控制台中执行相同的步骤会产生预期的结果:

aura_test.rb

require 'test_helper'

class AuraTest < ActiveSupport::TestCase

  def setup
    @champion = champions(:aura)
    @aura = Aura.new description: "Lorem ipsum Aura blah", champion_id: @champion.id, stat_type_id: 1
  end

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

end

aura.rb

class Aura < ApplicationRecord
  belongs_to :champion, optional: true
  belongs_to :stat_type
  belongs_to :affinity, optional: true
  belongs_to :location_group, optional: true
  validates :description, presence: true
  validates :champion_id, presence: true
end

champions.rb

class Champion < ApplicationRecord
  belongs_to :faction
  belongs_to :affinity
  belongs_to :rarity
  belongs_to :champion_type
  has_one :aura, dependent: :destroy
  validates :name, presence: true
end

赛程: 冠军.yml

  name: AuraChamp
  faction: one
  affinity: one
  rarity: one
  champion_type: one

当我在 Rails 控制台中运行类似代码时:

>> @champion = Champion.first
  Champion Load (0.1ms)  SELECT  "champions".* FROM "champions" ORDER BY "champions"."id" ASC LIMIT ?  [["LIMIT", 1]]
=> #<Champion id: 1, name: "", faction_id: 1, affinity_id: 1, rarity_id: 1, champion_type_id: 1, created_at: "2019-07-19 23:50:18", updated_at: "2019-07-19 23:50:18">
>> @aura = Aura.new description: "Lorem ipsum Aura blah", champion_id: @champion.id, stat_type_id: 1
=> #<Aura id: nil, name: nil, description: "Lorem ipsum Aura blah", champion_id: 1, stat_type_id: 1, affinity_id: nil, location_group_id: nil, increase: nil, is_percent: nil, created_at: nil, updated_at: nil>
>> @aura.valid?
  StatType Load (0.1ms)  SELECT  "stat_types".* FROM "stat_types" WHERE "stat_types"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
=> true

测试运行时我得到以下结果

FAIL["test_should_be_valid", AuraTest, 0.5133701380000275]
 test_should_be_valid#AuraTest (0.51s)
        Expected false to be truthy.
        test/models/aura_test.rb:11:in `block in <class:AuraTest>'

【问题讨论】:

  • 我不太了解 rails 或它的固定装置是如何工作的,但您可以在您的 champions 固定装置中添加一个 id 吗?或者也许命名那个夹具?它似乎与this question 有关。另外,我相信Champion.first 不会和champions(:aura) 一样,因为一个是从数据库中获取记录,另一个是使用你的fixture。
  • 使用调试器查看您的测试中@aura 存在哪些错误,因此@aura.errors.full_messages 您将得到问题的答案。

标签: ruby-on-rails automated-tests ruby-on-rails-5


【解决方案1】:

在您的模型中:

class Aura < ApplicationRecord
  belongs_to :champion, optional: true
  belongs_to :stat_type
  belongs_to :affinity, optional: true
  belongs_to :location_group, optional: true
  validates :description, presence: true
  validates :champion_id, presence: true  //<= Notice  this validation here
end

您对champion_id 进行了验证,这意味着您的记录在您为champion_id 提供值之前无效

现在看看你的灯具:

name: AuraChamp
  faction: one
  affinity: one
  rarity: one
  champion_type: one

您没有id,您在此处以@champion.id 的身份访问它

def setup
    @champion = champions(:aura)
    @aura = Aura.new description: "Lorem ipsum Aura blah", champion_id: @champion.id, stat_type_id: 1
end

您的 rails 控制台步骤正在通过,因为当记录保存到 数据库它会自动获得一个唯一的ID

试着像这样改变你的灯具:

name: AuraChamp
  id: 1
  faction: one
  affinity: one
  rarity: one
  champion_type: one

【讨论】:

  • 解决了。以上不是确切的答案,但结合@aura.errors.full_messages 上有关使用调试器的评论,让我得到了答案。问题是有另一个相关对象stat_type 没有在其固定装置中设置其ID。所以我要么需要将其建立为该对象的新实例,要么将适当的id: 添加到其固定装置中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-24
  • 2016-04-16
  • 2016-12-14
  • 2019-03-06
  • 2013-07-29
  • 1970-01-01
相关资源
最近更新 更多