0
0
Ruby on Railsframework~10 mins

Why testing is integral to Rails - Test Your Understanding

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

Complete the code to write a simple test method in Rails using Minitest.

Ruby on Rails
def test_[1]_is_valid
  assert true
end
Drag options to blanks, or click blank then click option'
Auser_creation
Bcontroller_response
Cmodel_validation
Ddatabase_connection
Attempts:
3 left
💡 Hint
Common Mistakes
Using spaces or special characters in the test method name.
Not starting the method name with 'test_'.
2fill in blank
medium

Complete the code to assert that a Rails model is invalid without a required attribute.

Ruby on Rails
user = User.new(name: nil)
assert_equal [1], user.valid?
Drag options to blanks, or click blank then click option'
Afalse
Bnil
Ctrue
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'true' instead of 'false' in the assertion.
Confusing the meaning of 'valid?' method.
3fill in blank
hard

Fix the error in the test that checks the response status of a controller action.

Ruby on Rails
get :index
assert_response [1]
Drag options to blanks, or click blank then click option'
A404
B302
C500
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 (not found) or 500 (server error) incorrectly.
Confusing redirect status 302 with success 200.
4fill in blank
hard

Fill both blanks to create a test that checks a model's attribute presence validation.

Ruby on Rails
test "[1] presence" do
  user = User.new([2]: nil)
  assert_not user.valid?
end
Drag options to blanks, or click blank then click option'
Aname
Bemail
Cpassword
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching the test name and attribute name.
Using an attribute that is not validated for presence.
5fill in blank
hard

Fill all three blanks to write a test that checks a controller redirects after creating a record.

Ruby on Rails
post :create, params: { user: { [1]: "test", [2]: "test@example.com" } }
assert_redirected_to [3]_path
Drag options to blanks, or click blank then click option'
Aname
Bemail
Cuser
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong attribute names in params.
Asserting redirect to an unrelated path.