Complete the code to define a basic model test class in Rails.
class UserTest < ActiveSupport::[1] end
The base class for model tests in Rails is ActiveSupport::TestCase.
Complete the code to test that a User model is invalid without an email.
test "should not save user without email" do user = User.new(email: nil) assert_not user.[1]? end
The valid? method checks if the model passes validations.
Fix the error in the test to check uniqueness validation of username.
test "username should be unique" do user1 = User.create(username: "testuser") user2 = User.new(username: "testuser") assert_not user2.[1]? end
To check if the new user is valid (fails uniqueness), use valid?.
Fill both blanks to test that a User model responds to name and email attributes.
test "user responds to attributes" do user = User.new assert_respond_to user, :[1] assert_respond_to user, :[2] end
The test checks if the user object has name and email methods.
Fill all three blanks to test that a User model is invalid without name, email, and password.
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
The test asserts the user is invalid and checks error messages for name and email fields.
