0
0
Ruby on Railsframework~10 mins

Model 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 define a basic model test class in Rails.

Ruby on Rails
class UserTest < ActiveSupport::[1]
end
Drag options to blanks, or click blank then click option'
ATestCase
BControllerTest
CIntegrationTest
DSystemTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using ControllerTest instead of TestCase
Using IntegrationTest for model tests
2fill in blank
medium

Complete the code to test that a User model is invalid without an email.

Ruby on Rails
test "should not save user without email" do
  user = User.new(email: nil)
  assert_not user.[1]?
end
Drag options to blanks, or click blank then click option'
Ainvalid
Bsave
Cpersisted
Dvalid
Attempts:
3 left
💡 Hint
Common Mistakes
Using save? which is not a method
Using invalid? which is not a method
3fill in blank
hard

Fix the error in the test to check uniqueness validation of username.

Ruby on Rails
test "username should be unique" do
  user1 = User.create(username: "testuser")
  user2 = User.new(username: "testuser")
  assert_not user2.[1]?
end
Drag options to blanks, or click blank then click option'
Avalid
Bsave
Ccreate
Dpersisted
Attempts:
3 left
💡 Hint
Common Mistakes
Using save? which is not a method
Using create which saves the record
4fill in blank
hard

Fill both blanks to test that a User model responds to name and email attributes.

Ruby on Rails
test "user responds to attributes" do
  user = User.new
  assert_respond_to user, :[1]
  assert_respond_to user, :[2]
end
Drag options to blanks, or click blank then click option'
Aname
Bemail
Cpassword
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Using password or username instead of name or email
5fill in blank
hard

Fill all three blanks to test that a User model is invalid without name, email, and password.

Ruby on Rails
test "user is invalid without required fields" do
  user = User.new(name: nil, email: nil, password: nil)
  assert_not user.[1]?
  assert_includes user.errors[:[2]], "can't be blank"
  assert_includes user.errors[:[3]], "can't be blank"
end
Drag options to blanks, or click blank then click option'
Avalid
Bname
Cemail
Dpassword
Attempts:
3 left
💡 Hint
Common Mistakes
Using save? instead of valid?
Checking errors on wrong attributes