I'd suggest that you get familiar first with Mocking and Moq, without using Ninject.Moq. It adds too much magic, which it greats, but to learn how it works it would be easier to take one product at a time.
Attached you will find a the AccountrControllerTest that ships as part of the MVC Apps template, but changed to use XUnit, Moq and Ninject.Moq. I hope this could helped, you can compare this approach to using manual mocking (that's what the original test class is doing) and MSTest to XUnit and Moq.
Notice that I manually created AccountController, since I don't want to mock that class. This could be harder to do if your class rely on dependency injection. To get the kernel to create instances of your classes under test just create a binding for that avoid getting a mock and then just resolve to that type.
_kernel.Bind<AccountController>().ToSelf();
_controller = _kernel.Get<AccountController>();
Still all of the dependencies will be mocked.
I didn't use it a lot, so I might be missing something here, but I found that I was writing a lot of setup code on the dependencies that I wanted to mock, so I didn't feel that Ninject.Moq was saving me a lot of time. I could change lines like this and completely get rid of Ninject.Moq:
_formsAutheniticationServiceMock = Mock.Get(_controller.FormsAuth);
for:
_controller.FormsAuth = _formsAutheniticationServiceMock = new Mock<IFormsAuthentication>();
Hi,
Could someone help me with usage? Where do you instanciate the
MockingKernel? Where do I bind?
Do I have to bind the MockingKernel such as
kernel.Bind<IRepository>().To<
FakeRepository>();
I also tried with:
private MockingKernel kernel;
private BandsController bandsController;
[SetUp]
public void Init()
{
kernel = new MockingKernel();
var repo = kernel.Get<IRepository>();
bandsController = new BandsController();
bandsController.Model = repo;
}
Mocking is still quite new to me, thanks for you help.
Teebot