Search⌘ K
AI Features

Writing Tests for Database Constraints

Explore how to create tests that directly verify database constraints in Rails apps. Learn how to use update_column to bypass model validations and check that constraints like price positivity are enforced. This lesson helps you ensure your data stays accurate by testing the database layer, improving app reliability.

We'll cover the following...

Test cases

Like all tests, tests for the correctness of the data model have a carrying cost. We don’t see a lot of value in testing null: false or unique: true because these tend to be easy to get right. Check constraints are more like real code and, thus, easier to mess up. We usually write tests for them. Let’s write a test for the constraint around the widget’s price. We’ll need two tests: ...