Unit Test with Ninject Instantiate

15,657

Solution 1

It looks to me like a typo of _giftService. In addition, attribute [TestInitialize] needs to be used in your constructor.

Try the following code by placing the correct service name _giftService - that your code is injecting instead:

var list = __giftService.FindAll();

Edit: Unit testing should be done without Ninject. Just create an instance of the object under test and inject a mock for every dependency manually.

Here is a sample with [TestInitialize]:

The unit test must have a default constructor:

[TestClass]
public class TestControllersHomeController
{
    private HomeController _sut;

    [TestInitialize]
    public void MyTestInitialize() 
    {
        var kernel = NinjectWebCommon.CreatePublicKernel();
        _sut = kernel.Resolve<HomeController>();
    }

    [TestMethod]
    public void TestIndex()
    {
        var result = _sut.Index();
        Assert.IsNotNull(result);
    }
}

Solution 2

The only way dependency injection is able to call your constructor and fill it with a parameter that has a value is if the dependency injection kernel is the one that instantiates your class.

In your case, MSTest is instantiating your test class, so Ninject does not have a chance to call your constructor.

To be honest, you are going about this the wrong way. You will battle MSTest if you pursue this further to try to get Ninject (or any other DI framework) to instantiate your test classes.

I would recommend you use 'new' to instantiate the class under test (_giftService = new GiftService();). If that class has dependencies in its constructor, use a mocking framework to pass in mocked version of those dependencies. This way you isolate your unit tests to only the functionality of the class under test.

Solution 3

_gift is null. Your variable is _giftService. You also should use the [TestInitialize] attribute for your constructor.

Extra tip:

You shouldn't create any dependencies in your unit test. So the giftService which you want to use should be a mock or a stub.

Share:
15,657
Overmachine
Author by

Overmachine

My Name is Emilio. I'm from the Dominican Republic SOreadytohelp

Updated on June 04, 2022

Comments

  • Overmachine
    Overmachine almost 2 years

    I'm trying to test my service using ninject and an unit test project with visual studio 2012. My inject works ok on my controllers, but when I try to do the same in the unit test class I get an Exception.

    System.NullReferenceException: Object reference not set to an instance of an object.

    namespace Trex.UnitTests
    {
        [TestClass]
        public class GiftServiceTests
        {
            private IGiftService _giftService;
    
            public void GiftServiceTest(IGiftService giftService)
            {
                 _giftService = giftService; 
            }
    
            [TestMethod]
            public void AddGift()
            {
    
                var list = _gift.FindAll();  <--- this line throw an exception
            }
        }
    }
    

    I think there is something wrong with the injection but I dont get it.