Search⌘ K
AI Features

Unit Test: Format Validation

Explore writing unit tests to enforce format validation in Rails, restricting inputs to letters and spaces. Understand how to define these tests, handle validation failures, and consider edge cases for robust code quality.

Defining the test

This unit test restricts the name field to only contain letters and spaces. To define this one, we’ll set a name containing an emoji character on the user and assert validation failure.

Here’s the test:

Ruby
context "when the name contains emojis" do
it "the user is invalid" do
user = User.new(name: "Hi 🤨")
user.valid?
expect(user.errors.messages).to include(:name)
end
end

Add ...