I use ninject widely with the factory extension. I also use moq to help with testing, and in some cases (especially complex view models in my MVVM MVVM application). I use the ninject kernel to simplify the automatic creation of mocks for the resources required by the tested class.
In my regular product code, the ninject factory extension makes it easy to simply declare an interface for the factory, which looks something like this:
public interface IMyViewModelFactory
{
MyViewModel CreateMyViewModel(int recordNum);
}
and the factory extension will automatically provide an implementation that maps the creation method to the constructor for the class when resolving any other dependencies that are constructor parameters from ninject. So, if my class has a constructor that looks like this:
public MyViewModel(ILogger logger, int recordNum)
{
_logger = logger;
_recordNum = recordNum;
}
then the factory extension will create an implementation of the factory method that will receive ILogger from the ninject kernel and go through my explicit parameter.
All this works fine for regular product code, but as far as I can tell, I can't get these things to work in my unit tests when I use a mocking kernel.
, , , . mocks , , factory. , factory , :
Kernel.GetMock<IMyViewModelFactory>()
.Setup(f => f.CreateMyViewModel(It.IsAny<ILogger>(), It.IsAny<int>()))
.Returns((ILogger logger, int recordNum) =>
new MyViewModel(Kernel.Get<ILogger>, recordNum);
, factory (, , - , , factory ), , .
ninject , , unit test . , , , , , factory , , . , , , , ( , , ), .
, - , - .
,