Unit Testing, Agile Development, Leadership & .NET - By Roy Osherove
Hi,
Love this code, thanks for sharing. I was wondering if you could shed some light into the following problem.
Imagine that the container is setup through a configuration file. I want to re-use this configuration file in my unit test project and selectively "override" dependancies with mock objects.
For example, consider IContractBusinessService, IContractDataService and IAuditDataService. My configuration file effectively does the following
UnityContainer container = new UnityContainer();
container
.RegisterType<IContractBusinessService, ContractBusinessService>()
.RegisterType<IAuditDataService, AuditDataService>()
.RegisterType<IContractDataService, ContractDataService>()
My unit test wants to test an instance of IBusinessService using a mock object for IContractDataService.
Using
container.RegisterInstance<IContractDataService>(mockObject);
does not seem to make a difference as a proper instance of IContractDataService is getting created regardless.
The same goes for the code provided here ...
I have got the functionality I was after by making a slight modification to the code you provided.
By changing
return (AutoMockingUnityContainer) this.RegisterType(typeof(T), null, null, new MockFactory(typeof(T),mocks,Lifetime.Singleton));
to
return (AutoMockingUnityContainer) this.RegisterType(typeof(T), typeof(T), null, new MockFactory(typeof(T),mocks,Lifetime.Singleton));
when I call WillCreateMockFor<T>() after I have already called RegisterType<T>() for the same T, the original registration is now "overwritten". Looks like this does not happen when you pass "null" so passing "typeof(T)" has done the trick.
Roy, thanks, this is great stuff!
Just wanted to mention that Angelos did find a bug in Unity - RegisterType is trumping RegisterInstance. I just checked in a fix, but in the meantime (for anyone else reading this) you can work around the problem by doing:
container.RegisterInstance<IContractDataService, IContractDataService>()
to erase the previous type mapping.
Hi Roy
What is the advantage of using unity over Rhino Mocks. I am pretty new to these technologies. Please throw some light on this subject.
Thanks
Gokul:
look here for a similar discussion and answers
tech.groups.yahoo.com/.../6699
Your container includes MockRepository but does not define it. What is the definition for MockRepository in your code?
Roy,
IMHO looking at the test you gave this doesn't look to me as AMC.
If you check out Elutian AMC you would see that there is no need for explicit
container.WillReturnAStubFor<ILogger>();
container.WillReturnAStubFor<IEmailer>();
on the begining of test.
IMHO, the whole purpose of AMC is to avoid having those lines (which are just shorter forms to register mocks).
AMC should get only the type, which constructor would be examined and those "inject needed mock to container" lines would be performed internaly inside of AMC.
I'm sure you already see the
blog.eleutian.com/.../TestsAutoMockingIoCContainer.aspx
but I still nated to put the link there because comparing their test example and yours clarifies my point :)
This is an interesting problem I've been running into at work. Sometimes we would try to write a test
How to build Microsoft Unity Auto Mocking container in less then 20 minutes of work… I have written before
Pingback from Dependency Injection Collection – Unity « Coding Tools, Reviews, and Solutions