title: Property inject in unittest with ninject
body: <p>I have a simple contoller of a standard MVC 5 web application. I installed the MVC 5 Ninject nuget package. I created a constructor and the interface that I have defined is injected. If I create a public property this is also injected.</p>
  
  <p>Now when I create a unittest project and want to test the mvc controller. How can I do that? I have to create a kernel. The kernel is created and if I get an instance of my IPersonComponent it is created. But why doesn't my property not get injected?</p>
  
  <p>I created two options and both I get nullreference exception...</p>
  
  <p>Interface:</p>
  
  <pre><code>public interface IPersonComponent
  {
      void DoSomething();
  }
  </code></pre>
  
  <p>Implementation:</p>
  
  <pre><code>public class PersonComponent : IPersonComponent
  {       public void DoSomething()
      {
          throw new NotImplementedException("The method is fired but not implemented");
      }
  }
  </code></pre>
  
  <p>TestInitialize:</p>
  
  <pre><code>private IKernel _kernel;
  
  [TestInitialize()]
  public void Initialize()
  {
      _kernel  = new StandardKernel();
  }
  </code></pre>
  
  <p><strong>Option 1</strong></p>
  
  <p>Controller that is working great:</p>
  
  <pre><code>public class HomeController : Controller
  {
      private readonly IPersonComponent _component;
  
      public HomeController(IPersonComponent component)
      {
          _component = component;
      }
  }
  </code></pre>
  
  <p>Unittest fails because PersonComponent is null:</p>
  
  <pre><code>[TestClass]
  public class HomeControllerTest
  {
      [Inject]
      public IPersonComponent PersonComponent { get; set; }
  
      [TestMethod]
      [ExpectedException(typeof(NotImplementedException))]
      public void Index()
      {
          // Arrange
          HomeController controller = new HomeController(PersonComponent);
  
          // Act
          ViewResult result = controller.Index() as ViewResult;
  
          // Assert
          Assert.IsNotNull(result);
      }
  }
  </code></pre>
  
  <p><strong>Option 2</strong></p>
  
  <p>Controller:</p>
  
  <pre><code>public class HomeController : Controller
  {
  
      [Inject]
      public IPersonComponent PersonComponent { get; set; }
  
      public HomeController()
      {
  
      }
  
      public ActionResult Index()
      {
          PersonComponent.DoSomething();
  
          return View();
      }
  }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>[TestMethod]
  [ExpectedException(typeof(NotImplementedException))]
  public void Index()
  {
      // Arrange
      HomeController controller = new HomeController();
  
      // Act
      ViewResult result = controller.Index() as ViewResult;
  
      // Assert
      Assert.IsNotNull(result);
  }
  </code></pre>
  
  <p><strong>This works:</strong>
  But why does it work? Why isn't it injected automatically?</p>
  
  <pre><code>[TestMethod]
  public void TestDoSomething()
  {
      var kernel = new StandardKernel();
  
      var comp = kernel.Get&lt;IPersonComponent&gt;();
      comp.DoSomething();
  }
  </code></pre>
  
  <p><strong>Edit based on the answer:</strong></p>
  
  <p><strong>Option 1</strong></p>
  
  <pre><code>    [TestMethod]
      [ExpectedException(typeof(NotImplementedException))]
      public void Index()
      {
          _kernel.Inject(this);
  
          // Arrange
          HomeController controller = new HomeController(PersonComponent);
  
          // Act
          ViewResult result = controller.Index() as ViewResult;
  
          // Assert
          Assert.IsNotNull(result);
      }
  </code></pre>
  
  <p><strong>Option 2</strong></p>
  
  <pre><code>[TestMethod]
  [ExpectedException(typeof(NotImplementedException))]
  public void Index()
  {
      // Arrange
      HomeController controller = _kernel.Get&lt;HomeController&gt;();
  
      // Act
      ViewResult result = controller.Index() as ViewResult;
  
      // Assert
      Assert.IsNotNull(result);
  }
  </code></pre>