Last Updated: February 25, 2016
·
508
· Alexandr K

Testing items selectors

Sometimes you have a controller that's picking up the items from the database. In one day someone will decide to add a sort with tests as well. But we can have tests that's probably it's not a good idea every time to change. Probably it's not a protip but it's good approach to have clean tests.

let!(:articles) {  2.times.map { create(:article) } }

before do
  get :index
end

it 'should pick up items' do
  collection = assigns(:articles)
  expect(collection).to have(2).items
  expect(collection).to include(articles[0])
  expect(collection).to include(articles[1])
end

Now it's possible to write extra test for checking default scope for sort as example instead of updating the existing tests.