gpt4 book ai didi

ruby-on-rails - 如何测试依赖: :destroy with RSpec?

转载 作者:数据小太阳 更新时间:2023-10-29 06:29:24 24 4
gpt4 key购买 nike

我想测试我的 User 模型关联 has_many :projects, dependent: :destroy 现在已经走了这么远:

it "destroys dependent projects" do
user = FactoryGirl.build(:user)
project = FactoryGirl.build(:project)

user.projects << project
user.destroy

expect(Project.count).to change(-1)
end

但这给出了一个错误:

Failure/Error: expect(Project.count).to change(-1)
ArgumentError:
`change` requires either an object and message (`change(obj, :msg)`) or a block (`change { }`). You passed an object but no message.

所以我认为 change 不是正确的匹配器,是吗?您能告诉我如何编写此测试而不会出现该错误吗?

最佳答案

你也可以使用 shoulda 匹配器:

it { expect(user).to have_many(:projects).dependent(:destroy) }

https://github.com/thoughtbot/shoulda-matchers

关于ruby-on-rails - 如何测试依赖: :destroy with RSpec?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28406717/

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