2013-06-20 6 views
9

を発生させますので、テストは失敗します。はRAISE_ERRORことを期待し、それは私が簡単なテストケース持って、このエラー

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)>' 

任意のアイデアなぜですか?

答えて

20

エラーをキャッチするには、ブロック内のコードをラップする必要があります。コードがエラーを予期していないスコープ内で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