Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ControllerTest instead of TestCase
Using IntegrationTest for model tests
✗ Incorrect
The base class for model tests in Rails is ActiveSupport::TestCase.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using save? which is not a method
Using invalid? which is not a method
✗ Incorrect
The valid? method checks if the model passes validations.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using save? which is not a method
Using create which saves the record
✗ Incorrect
To check if the new user is valid (fails uniqueness), use valid?.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using password or username instead of name or email
✗ Incorrect
The test checks if the user object has name and email methods.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using save? instead of valid?
Checking errors on wrong attributes
✗ Incorrect
The test asserts the user is invalid and checks error messages for name and email fields.