gpt4 book ai didi

ruby-on-rails - Rspec 测试 belongs_to 与 shoulda_matchers

转载 作者:太空宇宙 更新时间:2023-11-03 16:42:27 27 4
gpt4 key购买 nike

我的模型中有这段代码。

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

关于ruby-on-rails - Rspec 测试 belongs_to 与 shoulda_matchers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42555487/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com