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.

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

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