【发布时间】:2022-01-11 04:49:56
【问题描述】:
我正在从 Rails 4.2 升级到 6。我也在使用 FactoryBot 6.2.0。我有这个模型
class Book < ActiveRecord::Base
belongs_to :author, inverse_of: :book
…
validates :model, presence: true, unless: -> { author.check_vwf? }
我有一个 RSpec (rspec-rails 4.1.2 ) 测试,我想在其中测试关联……
describe Book do
…
it { should belong_to :author }
但运行此测试失败并出现以下错误。看起来“validates”方法正在运行,并且正在构建的实例没有“belongs_to”关联,但这正是我想要测试的……
Failure/Error: validates :model, presence: true, unless: -> { author.check_vwf? }
NoMethodError:
undefined method `check_vwf?' for nil:NilClass
升级我的 Rails 导致这个测试突然失败。给定版本的 Rails 和 RSpec 测试关联的正确方法是什么?
【问题讨论】:
-
请提供图书工厂的代码。另外,测试中是否创建了 book 对象?
-
“...”只是其他“it”关联检查。没有显式调用来创建对象的实例。
-
@Dave 你能用这两个模型更新你的问题吗?关联和
inverse_of似乎有问题 -
您能否提供有关您的工厂的详细信息以及您如何在规格中使用它们?你用过工厂机器人吗?
标签: ruby-on-rails unit-testing rspec associations ruby-on-rails-6