Test Validation Errors with RSpec
We can use RSpec unit tests to confirm model validations, like this:
But there's a false positive here; expect
returns true, but we can't be sure why it returned true. It may have been an invalid email, because the regex doesn't match bar
, or it may be invalid for many other reasons, such as an incomplete fixture.
Another method I re-learned today is this:
The first expect
triggers the validations; the second proves the right error was included. This makes a difference when flash messages or other code depend on your errors stack.