5

I have a factory that looks like below:

public IFoo GetFoo(IFile file)
{
  return _kernel.Get<IFoo>(new ConstructorArgument("file", file));
}

It works fine until I use Moq to mock IFoo. In the mock there is no constructor argument named file, and I get a Ninject.ActivationException.

How should I solve this?

magol
  • 6,135
  • 17
  • 65
  • 120
  • 4
    Something smells off, you shouldn't need to be using your IoC container in a unit test. – BFree Oct 05 '11 at 17:06
  • Agree with @BFree. Using the Ninject kernel within your factory is a bit of a smell too. See http://stackoverflow.com/questions/6277771/what-is-a-composition-root-in-the-context-of-dependency-injection – TrueWill Oct 05 '11 at 17:18
  • why you want to pass an argument when you want to mock an interface? – Ashley John Oct 05 '11 at 17:20
  • If I have understood everything right, ninject inject all depends when I create an object. But how do I do so, if I'm in my object needs to create a list of IFoo based on various conditions. I can use new Foo (file), but then I can not unit test it. – magol Oct 06 '11 at 07:25

1 Answers1

3

You should be mocking your factory during testing. Hopefully, the "GetFoo" method is part of your factory interface. Mock the factory and then you can set up the factory to return whatever IFoo you want (a test IFoo or perhaps a mock IFoo).

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
  • In my quest to get 100% test coverage, I tried to write unit tests for my factorys, but it was perhaps a bit over-ambitious – magol Oct 06 '11 at 20:00
  • If your factory simply delegates creation to ninject (or some other ioc container), then you really don't need to test your factories since, in effect, you're just testing your container. And hopefully, the container has already been tested! :) – PatrickSteele Oct 06 '11 at 20:29