Can't mock Unity Container call

2 posts, 0 answers
  1. Imaji
    Imaji avatar
    15 posts
    Member since:
    Apr 2007

    Posted 20 Sep 2010 Link to this post


    I'm trying to mock a RegisterType call on a mock Unity Container, but it's not being called.  I've recreated the test as follows:

            public void TestMethod1()
                IUnityContainer mockContainer = Mock.Create<IUnityContainer>();
                Mock.Arrange(() => mockContainer.RegisterType<IConcrete, Concrete>()).Returns(mockContainer);

                Class1 consumingClass = new Class1(mockContainer);


                Mock.Assert(() => mockCOntainer.RegisterType<IConcrete, Concrete>(), Occurs.Once());

    IConcrete and Concrete are empty, obviously Concrete implements IConcrete.

    Class1 is very simple as follows:

        public class Class1
            private readonly IUnityContainer _container;

            public Class1(IUnityContainer container)
                _container = container;

            public void RegisterStuffInUnity()
                var bob = _container.RegisterType<IConcrete, Concrete>();

    Any ideas?

  2. Ricky
    Ricky avatar
    467 posts

    Posted 22 Sep 2010 Link to this post

    Hi John,

    Thank you for submitting the issue. However I attached a test project created from the snippet you sent, where the test is passing. I tried both in debug and run mode and used Unity 2.0 for the purpose.

    Looking further into, I found out that if the test is preceded with following one, then only running both the tests fails the original one where the call never goes to mockContainer.RegisterType<,> . Here to add that the following test is executed first.

    public void Test2()
        ConsumingClass consumingClass = Mock.Create<ConsumingClass>();

    In this case, currently the workaround is to include the following Mock.Arrange to forcefully make it execute the original code block:

    Mock.Arrange(() => consumingClass.RegisterStuffInUnity()).CallOriginal();

    Finally, as we have already identified the issue, it will be patched in the next release. However if you want to give it an early try, we would love to send you a preview build to your telerik account.

    Kind Regards,
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
Back to Top