The least bad solution

Sometimes I look over the options and constraints to choose something suboptimal. I have to pick the least-bad solution.

I recently chose a least-bad way to write a test. In a Rails app, the most sensible thing to solve my problem was something like this:

def propagate_from_child_to_parent

In the test, I ended up having to write this assertion

expect_any_instance_of(ModelParents).to receive(:do_a_sideeffect)

This kind of stub and assertion is clearly a smell. But, consider the alternatives:

  • stub out the child model object under so that find_each returns a stub object that I can make sure do_a_sideffect is called on
  • try to hack around ActiveRecords associations so it returns the same object as I inject in my test
  • seek out some other result of do_a_sideeffect that I could assert on

In the end, it felt like the shady mock+assertion was the best choice. Using that particular assertion says “slow down and pay attention, a special thing is happening here”. It’s not something I want to do every time, but it was the least bad solution in this context.