Spec for has_many with dependent option
Every rails app almost certainly contains one or more
dependent option means that on deletion the foreign key gets
nullified. In many cases this is not the expected behaviour though. Even if it
is, it makes sense that we think about the behaviour of the relation for each
has_many definition. Not thinking about how the associated objects behave on
deletion often results in orphaned database entries and even in inconsistent
Adding a simple test to the spec suite asserts that every
association has explicitly defined the
First, we need two simple helper methods to get all our models and their
Then we should require these helper methods and expose them to our specs.
And finally, let’s add the spec that creates an example for each model.
The output could look something like the following in case we forget to define the option on one of the relations.
Now, every time we add a new model to our rails app and forget to define the
dependent option we get a failing spec.