← Back to Upcase

Asserting Setup code in a test case

When the setup code gets complicated, is it safe to add verification statements to your setup? So you can assert that the setup is correct, before moving to the code under test?

For example working in legacy code that has a poor test suite but is large, I want to call FactoryBot to create a Contact and set the first name but there are hundreds of callbacks on the Contact model and somewhere in the code my first name is getting replaced with nil. I waste a lot of time and figure out how to set the first name on the factory, so that it is persisted. Should I leave my assert to make sure that my test will clearly demonstrate when the setup is broken if something changes and not necessarily the code under test?

This is a random example that happened to me this week. I know that better designed code and factories will resolve the issue, but there is currently too much that is dependent on the factory in the legacy code’s poor test suite.

So I am generally asking if it is an anti-pattern to add asserts to my setup code in a test.