5 Comments

  • 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()
    .RegisterType()
    .RegisterType()

    My unit test wants to test an instance of IBusinessService using a mock object for IContractDataService.

    Using

    container.RegisterInstance(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() after I have already called RegisterType() 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.

  • 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
    http://tech.groups.yahoo.com/group/altdotnet/message/6699

  • Your container includes MockRepository but does not define it. What is the definition for MockRepository in your code?

Comments have been disabled for this content.