All threads / How to Test Validations in Rails Discussion

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to Test Validations in Rails Discussion

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.

Reply

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.

Reply
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,665+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.