Descriptive RSpec Tests
RSpec doesn’t always have the best error feedback. Oftentimes the message you receive is too verbose. I notice this a lot when dealing with ActiveRecord models. For example, let’s say we’re writing a new feature that allows us to sort a resource via an uploaded_on attribute.
Our spec may resemble:
context '.recently_uploaded_first' do
it 'sorts the collection so that recently uploaded pics are first' do
new = create(:picture, uploaded_on: Date.today)
old = create(:picture, uploaded_on: 2.days.ago)
middle = create(:picture, uploaded_on: Date.yesterday)
expect(Picture.recently_uploaded_first).to eq [new, middle, old]
end
end
Run this test prior to implementing the solution and RSpec will vomit an error message that fails to help us now let alone four months later when we have even less context.
So, let’s fix this and see if we can improve the readibility of our specs.
context '.recently_uploaded_first' do
it 'sorts the collection so that recently uploaded pics are first' do
create(:picture, uploaded_on: Date.today, description: "new")
create(:picture, uploaded_on: 2.days.ago, description: "old")
create(:picture, uploaded_on: Date.yesterday, description: "middle")
expect(Picture.recently_uploaded_first.map(&:description)).to eq ["new", "middle", "old"]
end
end
So when we run our test again we now get this:
Ah, much better. We know instantly that we have a sorting problem and if our implementation were to erroneously change in the future, we’ll know instantly what the problem is.
Thoughts?