Roadblock to Mocking Unit Tests
Posted
Tuesday, July 15, 2008 11:58 AM
by
cromwellryan
I've been a faithful unit tester for a few years now. I may not do everything by the book (I think end-to-end unit tests are helpful), but I do get good coverage most of the time. That said, I've found myself unable to use any of the Mock frameworks out there, because I don't use your typical Dependency Injection pattern. That is, I don't like "building up" very freaking instance with all the "Provider" or "Service" interfaces I'm might need in the possible life of a business object. Yes, if your Customer class is only going to create or save an Order, it feels OK, but my objects are busy dude, so I'd need the longest constructor of all time.
I prefer Dependency Resolution. I don't use any fancy framework, I just use a good old, DI.Resolve<IBusyBee>() and go about my business. It works wonderfully, it's light-weight (cause I don't use configuration based DI frameworks), and it's my baby.
That said, it's also been the roadblock to my mocking foray. That was, until I "got it" today. Long story short, here's how I roll:
Create a Mock DI Container...
Mock<IDIContainer> mockContainer = new Mock<IDIContainer>();
Create the Injected Mock instances...
Mock<INotificationManager> mockNotifMgr = newMock<INotificationManager>();
Mock<IRepository> mockRepository = newMock<IRepository>();
Tell the mock IDIContainer to return the mock instances when asked...
mockContainer.Expect(c => c.Resolve<INotificationManager>(null)).Returns(mockNotifMgr.Object);
mockContainer.Expect(c => c.Resolve<IRepository>(null)).Returns(mockRepository.Object);
Mock your heart out...
mockNotifMgr.Expect(mgr => mgr.NotifyEmailConfirmation(It.IsAny<Member>())).AtMostOnce();
Member.Register("username", "firstname", "lastname", "ryan@myus.com");
mockContainer.VerifyAll();
mockNotifMgr.VerifyAll();
I'm very happy to have finally jumped on the Moq bandwagon as it will certainly make some unit tests much easier to write. Maybe I'll even do the test first part more faithfully now.
FYI, I use my own DI framework which is just a glorified HashTable. It works for me and it is quick. Moq has also become my Mock framework of choice for no good reason other than I downloaded it first.