0
0
Ruby on Railsframework~10 mins

Controller tests in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a GET request to the index action in a Rails controller test.

Ruby on Rails
get [1]
Drag options to blanks, or click blank then click option'
Ausers_url
Bshow_url
Cedit_url
Dnew_url
Attempts:
3 left
💡 Hint
Common Mistakes
Using show_url instead of users_url
Using post instead of get
Forgetting to call the URL helper
2fill in blank
medium

Complete the code to check that the response was successful in a Rails controller test.

Ruby on Rails
assert_response [1]
Drag options to blanks, or click blank then click option'
A:success
B:redirect
C:error
D:missing
Attempts:
3 left
💡 Hint
Common Mistakes
Using :redirect when expecting success
Using :error which means failure
Using :missing which means 404 not found
3fill in blank
hard

Fix the error in the test code to correctly check that a new record was created after a POST request.

Ruby on Rails
assert_difference('[1].count', 1) do
  post users_url, params: { user: { name: 'Alice' } }
end
Drag options to blanks, or click blank then click option'
Auser
Busers
CUsers
DUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or plural names for the model
Using variable names instead of class names
4fill in blank
hard

Fill both blanks to test that the show action renders successfully for a given user.

Ruby on Rails
get [1]([2])
assert_response :success
Drag options to blanks, or click blank then click option'
Auser_url
Buser
Cpost_url
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using post_url instead of user_url
Passing the model class instead of an instance
5fill in blank
hard

Fill all three blanks to test updating a user's name and checking the redirect.

Ruby on Rails
patch [1], params: { id: [2].id, user: { name: 'Bob' } }
assert_redirected_to [3]
Drag options to blanks, or click blank then click option'
Auser_url(user)
Buser
C:user
Dusers_url
Attempts:
3 left
💡 Hint
Common Mistakes
Using users_url instead of user_url(user)
Using symbol instead of URL helper for redirect
Passing class instead of instance for id