Nerdy tidbits from my life as a software engineer

Wednesday, December 17, 2008

The Problem With The Chicken And Egg

Today I created a mock object in my framework whose constructor happens to call one of its own methods (just a property setter – no big deal – but it still calls it).  Before expectations can be set on the mock object instance, it must be instantiated, which means that the base class’ constructor is invoked.  However, because the mock hasn’t been created yet, there is no way to set an expectation on a method that the constructor is calling before it is created.  Which means that any object whose constructor invokes methods on itself is in a bit of a bind.  It can’t be created as things are right now without throwing an expectation violation.

One way to handle this problem is to use a partial mock instead of a regular mock.  This does solve the problem, since all of the method invocations pass down to the base class until you can set expectations on them.  But it made me wonder, should I put some funky code in the constructor code that I generate to handle this automatically?  I can’t remember what Rhino or Moq does, or what the expected behavior should be.  I suspect that they would barf, too…in which case, I’ll probably just imitate their implementation and keep my stuff behaving the same way.  But it does seem restrictive to either disallow methods to be called in the constructor of a class you want to mock, or to have no choice but use partial mocks for these situations.  Partials can be dangerous since they won’t catch cases where methods that shouldn’t be called are, in fact, called.

2 comments:

Ayende Rahien said...

rhino mocks will deal with just work

Michael J. Braude said...

What do you mean 'just work'? Do you mean that it will automatically pass the method calls down to the base class until the constructor is finished executing?

Wouldn't I maybe want the ability to mock up these method calls before I invoke the constructor? Or at least have the option to enforce constraints or not during creation?