【发布时间】:2013-12-31 17:10:43
【问题描述】:
我正在尝试使我当前的规范更具体,以测试 Thing.Others 的独特性/独特性
class Thing < ActiveRecord::Base
has_and_belongs_to_many :others, -> { distinct }
end
describe Thing do
it { should have_and_belong_to_many(:others).x }
end
我目前对 x 没有任何帮助:规范通过了,我可以在其他规范中有效地创建和保存来自另一个类的事物。
当我用distinct 替换x 时,我得到NoMethodError: undefined method 'distinct' for #<Shoulda::Matchers::...。
如果我将{ distinct } 替换为{ where(distinct: true) } 并将conditions(distinct: true) 放入x,则验证通过,但其他规范无法正确保存Thing 类。
【问题讨论】:
-
我不确定我是否完全理解第 6 行的“x”是什么。至于 NoMethodError,也许您需要定义一个自定义匹配器或包含要在您的规范中使用的匹配器
spec_helper文件? -
我使用 x 作为占位符,这样更容易描述我尝试过的其他规范。我对此很陌生。是否有替换不同代码段的约定?
标签: ruby-on-rails validation associations rspec-rails shoulda