Ruby on rails 3 预计将引发_错误,测试将失败,因为它会引发此错误

Ruby on rails 3 预计将引发_错误,测试将失败,因为它会引发此错误,ruby-on-rails-3,rspec,Ruby On Rails 3,Rspec,我有一个简单的测试用例: 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

我有一个简单的测试用例:

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)>'
1)删除关联板时,链接被删除
失败/错误:预期(Link.find(Link.id))。引发_错误ActiveRecord::RecordNotFound
ActiveRecord::RecordNotFound:
找不到id为1的链接
#./spec/models/link_spec.rb:47:in'block(2层)in'

知道为什么吗?

要捕获错误,需要将代码包装在块中。您的代码正在不应出现错误的范围内执行
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