...

/

Unit Test: Format Validation

Unit Test: Format Validation

Let’s learn to write a unit test to implement a format constraint.

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:

Press + to interact
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 ...