Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Test ActiveRecord Validations

    Victor HazbunVictor Hazbun
    railsRuby on Rails

    Learn how to test ActiveRecord validations using RSpec.

    In this lesson we'll write a test that expects the name attribute to be present on the User model instance. We'll see that at first this test fails because we need to add this validation on the model.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: 0:00 Let's test the username presence validation. When the name attribute is blank, it should have errors on the name attribute. We will need to set up a user instance. The name attribute must be blank.

    0:15 Next, we trigger the validation on the user instance by calling the valid method. This will also set the errors on the name attribute. We expect the name attribute errors to be "Can't be blank."

    0:32 Now let's run the text to see them fail. The test failed because we have not implemented the validation on the user model.

    0:40 To validate name presence, we call validate followed by the attribute name with the option presence true. If we run the test again, they pass because we implemented the presence validation on the name attribute.