Is there a built-in way of specifying asserts in Rails that will throw an exception if an invariant is broken during development and testing?
Edit: Just to be clear, I’m looking for asserts that can be placed in models or controllers as opposed to asserts that you would use for unit tests.
There are many assert functions if you are writing tests. But for assertiona in the main code, there aren’t any and you can roll your own easily.
Add something like this to
environment.rb:and make it a no-op in your
environments/production.rbso there is minimal overheadThen, in your code, you can assert to your heart’s content:
If
valuedoes not equalexpected_valueand you aren’t running in production, an exception will be raised.