Cleaner RSpec Controller Specs
RSpec, dev, ruby, testingTesting is an important part of software development. Having a solid test suite allows you to develop more confidently – it enables you to make large refactorings without fear of unintended consequences. I test almost all the code I write.
Recently, I found a bug that allowed users to see ‘inactive’ products; so I wrote a test to prevent that. I’ve seen lots of specs that test active products like so:
1
2
3
4
5
6
7
8
9
it "scopes products to active and available" do
not_active = Factory(:product, :active => false)
not_available = Factory(:product, :available => false)
active_available = Factory(:product, :active => true, :available => true)
get :index
assigns(:products).should_not include(not_active)
assigns(:products).should_not include(not_available)
assigns(:products).should include(active_available)
end
Tests like this are wordy and can lead to a ton of factoried objects. There’s got to be a better way!!


