Search⌘ K
AI Features

Testing What Rails Give Us

Explore how to test Rails models effectively by focusing on functionality rather than implementation. Learn to use TDD principles, test associations in use, validate object states, and apply custom RSpec matchers. Understand the role of shoulda-matchers and how to handle complex validations with practical examples.

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 ...