I thought I'd start a series of short blog posts on things I remark on during code review that could be of wider interest.
Do you see anything to improve in this extract from a feature spec, assuming we're fine with doing assertions against the DB?
fill_in "Title", with: "My title"
click_link "Create item"
item = Item.last
expect(item.title).to eq "My title"
What happens if
item is nil? The test will explode on the last line with
NoMethodError: undefined method 'title' for nil:NilClass.
If we would instead do
item = Item.last!
then it would explode on that line, with
That's a less cryptic error that triggers earlier, at the actual point where your assumption is wrong.
Active Record's FinderMethods also include