Testing What Rails Give Us

Learn about associations, validations, functional testing, and how testing the Rails benefit us.

Rails provide built-in functionality for associations and validations. Let’s see how to effectively and usefully test those features in our application.

In both cases, we go back to the basic principle that we’re testing functionality and not implementation. Although we cannot normally write tests to show the existence of a particular association or validation, we do sometimes write tests that show those features in action. For associations, this means showing the association in use. For validations, it means testing the overall logic of what makes an instance valid.

Associations and validations

The testing gem shoulda-matchers defines matchers that specifically test for the existence of validations and associations. Let’s see that in action below:

Get hands-on with 1200+ tech skills courses.