PDA

View Full Version : reconfigure objects, NMock


madism
02-21-2007, 08:59 AM
Hello,

I am using NMock2 in unit tests. On occasion I run into the problem that I am testing classes that need to use the service locator pattern, ContextRegistry.GetContext().GetObject(..). (for instance WF activities and other MS framework constructed code).

I can of course create a stub instead of a mock for every specific test, but I would rather use generated mocks since it is a lot less work. What I would like to be able to do is reconfigure objects or object factories at runtime, so I can switch out the objects that would normally be returned by GetObject with a Mock generated dynamic proxy. I have tried to do this with ConfigureObject(..), but it seems that once an object has been configured in XML, you cannot 'reconfigure' it with this method. Any chance this can be changed or that a feature can be added? Or is there a lower level API that I can use?

Thanks

Aleks Seovic
02-26-2007, 08:46 PM
You can register configured object instance directly, by calling IConfigurableObjectFactory.RegisterSingleton() method directly.

You can also create new application context, containing only objects you need, manually, and register it with the ContextRegistry (of course, you'd have to clear ContextRegistry between the tests).

- Aleks