Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Reseting Defaults in StructureMap when Injecting Stubs

This is just a short FYI post because this caused me some headache recently.

One thing to keep in mind when you are using StrucutreMap and are Injecting Stubs into the ObjectFactory, you will want to reset the ObjectFactory engine after you done with your tests.

To do this you will need to add this line of code.
ObjectFactory.ResetDefaults();

How you implement this and where you put it is up to you.  I simply put this as the last line of code in my tests, but you could put it in [TestFixtureTearDown] or if you wanted you could wrap your entire test in a try-finally block and put it there (I did not do this, did not think it was needed).

BTW, the error message you will receive if you have stubs lingering around in your ObjectFactory is the following ‘This action is invalid when the mock object is in verified state’.

Till next time



About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Red-Gate!