我有这个代码在我的模型。Rspec测试belongs_to与shoulda_matchers
belongs_to :user, -> { includes :user_profile }
我通过shoulda doc去,我没有发现任何方式测试含proc
includes
一部分。请帮忙。
我有这个代码在我的模型。Rspec测试belongs_to与shoulda_matchers
belongs_to :user, -> { includes :user_profile }
我通过shoulda doc去,我没有发现任何方式测试含proc
includes
一部分。请帮忙。
假设有问题的型号被命名为Car
(你有没有贴封装类的,所以我完全做这件事的名字),你可以测试它:
describe 'user' do
let(:car) { create_car_somehow }
it 'preloads the user_profile association' do
expect(car.user.association(:user_profile)).to be_loaded
end
end
#association
回报的元数据指定的关联(类型ActiveRecord::Associations::BelongsToAssociation
等)。返回的对象响应#loaded?
,如果关联已被加载,则返回true
,否则返回false
。
哪里定义了be_loaded?你可以发布一个链接到文档? – Anthony
这是一个动态定义的动态[predicate matcher](https://www.relishapp.com/rspec/rspec-expectations/v/2-2/docs/matchers/predicate-matchers)。基本上,'expect(object).to be_foo'自动转换为'expect(object.foo?)。be_truthy'。 –
没有内置的AFAIK –