Ninject.Moq

277 views
Skip to first unread message

Assaf Stone

unread,
Sep 3, 2009, 3:01:16 AM9/3/09
to ninject
Hi.

Does anybody here use Ninject.Moq (http://github.com/enkari/
ninject.moq/tree/master)?

Does anybody have an example of usage?

Thanks,
Assaf.

Miguel

unread,
Sep 6, 2009, 2:58:57 AM9/6/09
to ninject
I started using Ninject.Moq for an MVC app, but I just got stuck when
trying to setup the Mock. All the mocked objects get injected as
dependencies, it works great, but then I need to setup the mocks so
they actually do something and I can't get back from the mocked object
to the mock.

Anyone has use this?

Miguel Madero

unread,
Sep 6, 2009, 6:58:30 AM9/6/09
to ninject
I just realized that Mock can do this using Mock.Get that I can use on the mocked objects to obtain back the mock.
--
Miguel A. Madero Reyes
www.miguelmadero.com (blog)
m...@miguelmadero.com

Miguel Madero

unread,
Feb 21, 2010, 5:52:37 PM2/21/10
to ninject
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>();






--
Miguel A. Madero Reyes
www.miguelmadero.com (blog)
m...@miguelmadero.com

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

AccountControllerTests.cs

teebot

unread,
Feb 25, 2010, 10:51:56 AM2/25/10
to ninject
Indeed your example was very useful and I took your advice to first
use Moq without ninject.moq.
I already wrote tests that rely on moq and used kernel.inject() as
shown in your example. It works perfectly.

Thank you so much Miguel.

>  AccountControllerTests.cs
> 14KViewDownload

Miguel Madero

unread,
Feb 25, 2010, 5:39:22 PM2/25/10
to nin...@googlegroups.com
:)
Reply all
Reply to author
Forward
0 new messages