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

JustMock and Ninject....

1 Answer 72 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 09 Oct 2012, 02:29 PM
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?

1 Answer, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 11 Oct 2012, 09:08 PM
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.

Tags
General Discussions
Asked by
Chris
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or