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
Create an account Log in

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

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

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