lundi 21 mars 2016

How to test if database records are ordered by id?

I'm testing a JSON API. And up until recently I assumed the records are returned ordered by id. But when Posts.all turned into Posts.where(user_id: params[:user_id]), it broke the following test:

it 'takes "limit" param' do
  post1 = create :post
  post2 = create :post, user_id: post1.user_id
  get user_posts_path(post1.user_id, limit: 1)
  expect(response.body).to be_json_eql([{id: post1.id}].to_json).including(only: :id)
end

I can probably add the following test:

it 'returns records ordered by id' do
  post1 = create :post
  post2 = create :post, user_id: post1.user_id
  get user_posts_path(post1.user_id)
  expect(response.body).to be_json_eql([{id: post1.id}, {id: post2.id}].to_json).including(only: :id)
end

but it succeeds without adding .order(:id) to the controller. What do I do?

Aucun commentaire:

Enregistrer un commentaire