Skip to main content

How to Test Validations in Rails Discussion

General • Asked by Chris Oliver

Where do you think test factories come in here? A RatingFactory could make sure that the object instance we're using for tests is valid without us having to care how it's achieved.


For validations, I sometimes like to use the shoulda-matchers gem

It's pretty cool because alot of use cases are set up already so it saves having to come up with the edge cases.

For example, in a model test (assuming Rails default not RSpec)
should validate_presence_of(:attribute)

For Rspec, you would write

it { should validate_presence_of(:attrinute) }

Obviously, it doesn't apply in some cases such as custom validate method but it's always a good start into a large codebase that might not have a massive test coverage.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.