I am working on a large desktop application. The application communicates with number of external services and application is using Unity container to resolve the objects. I am facing issue in writing unit test using rhino mock. I am mocking those services that communicates with external services. I have a class “HatAgent.cs” that communicates with external service. I mock this class in my unit test. The life time scope of “HatAgent.cs” is singleton.
If i have different unit test classes and in one class if i mock “HatAgent.cs” and override it like below:
var agent = MockRepository.GenerateMock
(); //Stub methods..... IocContainerFactory.Instance.OverrideRegistrations(c => c.RegisterInstance(typeof(IHatAgent), agent ));
It works fine when i run unit test of that classe. But if i mock this in different unit test classes and when run all unit test together, my test fails. The reason in my methods when IHatAgent resolves, it gets the mock object of those unit test class which constructor runs last.
How to handle this?
Also, how unit test order runs?