gpt4 book ai didi

ruby-on-rails-3 - 期望 raise_error 并且测试失败,因为它引发了这个错误

转载 作者:行者123 更新时间:2023-12-04 01:33:47 26 4
gpt4 key购买 nike

我有简单的测试用例:

it "is removed when associated board is deleted" do
link = FactoryGirl.create(:link)
link.board.destroy
expect(Link.find(link.id)).to raise_error ActiveRecord::RecordNotFound
end

它失败并输出:
1) Link is removed when associated board is deleted
Failure/Error: expect(Link.find(link.id)).to raise_error ActiveRecord::RecordNotFound
ActiveRecord::RecordNotFound:
Couldn't find Link with id=1
# ./spec/models/link_spec.rb:47:in `block (2 levels) in <top (required)>'

知道为什么吗?

最佳答案

要捕获错误,您需要将代码包装在一个块中。您的代码正在执行 Link.find(link.id)在不期望错误的范围内。适当的测试:

it "is removed when associated board is deleted" do
link = FactoryGirl.create(:link)
link.board.destroy
expect { Link.find(link.id) }.to raise_error ActiveRecord::RecordNotFound
end

关于ruby-on-rails-3 - 期望 raise_error 并且测试失败,因为它引发了这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17212754/

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