@@ -302,20 +302,31 @@ def test_sorting_by_multiple_fields
302302 assert_equal '14' , json_response [ 'data' ] [ 0 ] [ 'id' ]
303303 end
304304
305+ def create_alphabetically_first_user_and_post
306+ author = Person . create ( name : "Aardvark" , date_joined : Time . now )
307+ author . posts . create ( title : "My first post" , body : "Hello World" )
308+ end
309+
305310 def test_sorting_by_relationship_field
306- get :index , { sort : 'author.name' }
311+ post = create_alphabetically_first_user_and_post
312+ get :index , params : { sort : 'author.name' }
307313
308314 assert_response :success
309- assert json_response [ 'data' ] . length > 10
310- assert_equal '17' , json_response [ 'data' ] [ 0 ] [ 'id' ]
315+ assert json_response [ 'data' ] . length > 10 , 'there are enough recordsto show sort'
316+ assert_equal '17' , json_response [ 'data' ] [ 0 ] [ 'id' ] , 'nil is at the top'
317+ assert_equal post . id . to_s , json_response [ 'data' ] [ 1 ] [ 'id' ] , 'alphabetically first user is second'
311318 end
312319
313320 def test_desc_sorting_by_relationship_field
321+ post = create_alphabetically_first_user_and_post
314322 get :index , { sort : '-author.name' }
315323
316324 assert_response :success
317- assert json_response [ 'data' ] . length > 10
318- assert_equal '17' , json_response [ 'data' ] [ -1 ] [ 'id' ]
325+ assert json_response [ 'data' ] . length > 10 , 'there are enough records to show sort'
326+ assert_equal '17' , json_response [ 'data' ] [ -1 ] [ 'id' ] , 'nil is at the bottom'
327+ assert_equal post . id . to_s , json_response [ 'data' ] [ -2 ] [ 'id' ] , 'alphabetically first user is second last'
328+ end
329+
319330 end
320331
321332 def test_invalid_sort_param
0 commit comments