JustMock and Ninject....

2 posts, 1 answers
  1. Chris
    Chris avatar
    21 posts
    Member since:
    Oct 2012

    Posted 09 Oct 2012 Link to this post

    I am sure I am doing something wrong but I can't for the life of me figure it out.... (of course it doesn't help that I can't debug the application reliably with the profiler enabled....)
    So here is my challenge and hopefully someone can help me with it...
    I have the following production method:
    public TResult CreateEmptyEntity<TResult>() where TResult : IBlockEntity
            {
                var result = DependencyInjector.Get<TResult>();
     
                var setupEntity = (result as IBlockEntitySetup);
                if (setupEntity == null)
                {
                    throw new EntitySetupNotSupportedException(
                        "Entity type requested does not support construction through this method.");
                }
                return result = setupEntity.CreateEmptyEntity‎<TResult>();
            }

    This method is using Ninject (as an internal property to this class (DependencyInjector is of type IKernel) to get the appropriate object to use for result....

    I am trying to unit test this method with the following method:
    [TestFixture]
        public class ProtocolABlockTranslatorTestBase
        {
            protected ProtocolABlockTranslator Target;
            protected IKernel KernelMock;
     
            [SetUp]
            public void BaseInitializer()
            {
                KernelMock = new StandardKernel();
                Target = Mock.Create<ProtocolABlockTranslator>(Constructor.Mocked, Behavior.CallOriginal);
            }
     
        }
     
    [TestFixture]
        public class CreateEmptyEntityTests : ProtocolABlockTranslatorTestBase
        {
            [SetUp]
            public void MyInitialization()
            {
            }
     
            [Test]
            public void Test0010DependencyInjectionGetMethodCalledOnceWithCorrectType()
            {
                KernelMock.Bind<ITowerConfiguration>().To<MockedTowerConfigSetup>();
                Mock.Arrange(() => KernelMock.Get<ITowerConfiguration>()).OccursOnce();
     
                Target.DependencyInjector = KernelMock;
     
                Target.CreateEmptyEntity<ITowerConfiguration>();
     
                Mock.Assert(KernelMock);
            }
        }

    Unfortunately its not working..  I am encountering my custom exception inside the if block (although when I look at result it equals null as I am attempting to debug (which is not working right due to the profiler being enabled).

    I am just trying to verify that a call to my production method with a certain templated type then results in a call to get with the same type.
    Any thoughts?

  2. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 11 Oct 2012 Link to this post

    Hi Chris,
    Thanks again for contacting us. The Get method on StandardKernel is basically an extension method which is why asserting KernelMock wont work here.

    I wrote the test in the following way and it is working as expected:

    [TestInitialize]
    public void BaseInitializer()
    {
        KernelMock = new StandardKernel();
        Target = Mock.Create<ProtocolABlockTranslator>(Constructor.Mocked, Behavior.CallOriginal);
    }
     
    [TestMethod]
    public void Test0010DependencyInjectionGetMethodCalledOnceWithCorrectType()
    {
        var mockTowerConfiguration = Mock.Create<ITowerConfiguration>();
     
        Mock.Arrange(() => KernelMock.Get<ITowerConfiguration>()).Returns(mockTowerConfiguration).OccursOnce();
     
        Assert.AreEqual(mockTowerConfiguration, KernelMock.Get<ITowerConfiguration>());
         
        Mock.Assert(() => KernelMock.Get<ITowerConfiguration>());
    }

    However, I noticed that if I try to do a negative assertion (not calling KernelMock.Get<>) test then it is not throwing the exact exception.Good news is that It is already fixed and will be included in the Q3 build next week. 

    Hope this resolves your issue.

    Kind Regards
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft R3 2016 release webinar banner
Back to Top