I have simple test case:
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
And it fails with output:
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)>'
Any idea why ?
Use let to define a memoized helper method. The value will be cached across multiple calls in the same example but not across examples. Note that let is lazy-evaluated: it is not evaluated until the first time the method it defines is invoked.
Summary: RSpec's subject is a special variable that refers to the object being tested. Expectations can be set on it implicitly, which supports one-line examples. It is clear to the reader in some idiomatic cases, but is otherwise hard to understand and should be avoided.
To catch error you need to wrap code in a block. Your code is executing Link.find(link.id)
in the scope that is not expecting error. Proper test:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With