title: How to test Ninject ConstructorArguments using MOQ objects?
body: <p>I have been doing my first Test Driven Development project recently and have been learning Ninject and MOQ. This is my first attempt at all this. I've found the TDD approach has been thought provoking, and Ninject and MOQ have been great. The project I am working on has not particularly been the best fit for Ninject as it is a highly configurable C# program that is designed to test the use of a web service interface. </p>
  
  <p>I have broken it up into modules and have interfaces all over the shop, but I am still finding that I am having to use lots of constructor arguments when getting an implementation of a service from the Ninject kernel. For example;</p>
  
  <p>In my Ninject module;</p>
  
  <pre><code>Bind&lt;IDirEnum&gt;().To&lt;DirEnum&gt;()
  </code></pre>
  
  <p>My DirEnum class;</p>
  
  <pre><code>public class DirEnum : IDirEnum
  {
      public DirEnum(string filePath, string fileFilter, 
          bool includeSubDirs)
      {
          ....
  </code></pre>
  
  <p>In my Configurator class (this is the main entry point) that hooks all the services together;</p>
  
  <pre><code>class Configurator
  {
  
      public ConfigureServices(string[] args)
      {
          ArgParser argParser = new ArgParser(args);
          IDirEnum dirEnum = kernel.Get&lt;IDirEnum&gt;(
              new ConstructorArgument("filePath", argParser.filePath),
              new ConstructorArgument("fileFilter", argParser.fileFilter),
              new ConstructorArgument("includeSubDirs", argParser.subDirs)
          );
  </code></pre>
  
  <p>filePath, fileFilter and includeSubDirs are command line options to the program. So far so good. However, being a conscientious kind of guy, I have a test covering this bit of code. I'd like to use a MOQ object. I have created a Ninject module for my tests;</p>
  
  <pre><code>public class TestNinjectModule : NinjectModule
  {
      internal IDirEnum mockDirEnum {set;get};
      Bind&lt;IDirEnum&gt;().ToConstant(mockDirEnum);
  }
  </code></pre>
  
  <p>And in my test I use it like this;</p>
  
  <pre><code>[TestMethod]
  public void Test()
  {
      // Arrange
      TestNinjectModule testmodule = new TestNinjectModule();
      Mock&lt;IDirEnum&gt; mockDirEnum = new Mock&lt;IDirEnum&gt;();
      testModule.mockDirEnum = mockDirEnum;
      // Act
      Configurator configurator = new Configurator();
      configurator.ConfigureServices();
      // Assert
  
      here lies my problem! How do I test what values were passed to the
      constructor arguments???
  </code></pre>
  
  <p>So the above shows my problem. How can I test what arguments were passed to the ConstructorArguments of the mock object? My guess is that Ninject is dispensing of the ConstuctorArguments in this case as the Bind does not require them? Can I test this with a MOQ object or do I need to hand code a mock object that implements DirEnum and accepts and 'records' the constructor arguments?</p>
  
  <p>n.b. this code is 'example' code, i.e. I have not reproduced my code verbatim, but I think I have expressed enough to hopefully convey the issues? If you need more context, please ask!</p>
  
  <p>Thanks for looking. Be gentle, this is my first time ;-)</p>
  
  <p>Jim</p>