Testing Database Constraints

When treating your SQL database as a “dumb store” (or when using an RDBMS that lacks the sophisticated features of Postgres), you’d typically use various features of Active Record to ensure database integrity and you’d naturally want to test that code. Although we’re using constraints to enforce database integrity (like the check constraint from Exposing the Vulnerability Devise and Rails Leave Open), we’d still like to have test coverage that the constraint is doing what we want.

We can easily test this constraint in RSpec, but it requires a somewhat cumbersome assertion mechanism using exceptions. First, we’ll see how this works, and then we’ll create an RSpec matcher to abstract the awkward syntax away so ...

Get Rails, Angular, Postgres, and Bootstrap 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.