This is a migrated thread and some comments may be shown as answers.

Can't mock Unity Container call

1 Answer 99 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Imaji
Top achievements
Rank 1
Imaji asked on 20 Sep 2010, 12:06 PM
Hi,

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:

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

            Class1 consumingClass = new Class1(mockContainer);

            consumingClass.RegisterStuffInUnity();

            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?

Thanks,
John

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 22 Sep 2010, 01:16 PM
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.

[TestMethod]
public void Test2()
{
    ConsumingClass consumingClass = Mock.Create<ConsumingClass>();
    consumingClass.RegisterStuffInUnity();
}

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
Tags
General Discussions
Asked by
Imaji
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or