Testing What Rails Gives You

Rails provides built-in functionality for associations and validations, which leads to questions about how to effectively and usefully test those features in your application.

The answer in both cases is similar, and goes back to the basic principle that we’re testing functionality and not implementation. Although I do not normally write tests just to show the existence of a particular association or validation, I 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.

The testing gem shoulda-matchers defines matchers that specifically test for the existence of validations ...

Get Rails 5 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.