Mocking Ninject IKernel

237 views
Skip to first unread message

M

unread,
Dec 31, 2012, 3:55:30 AM12/31/12
to rhino...@googlegroups.com
Hi

This is the block of code that I have written to test some method. But it throws an exception on line 3 that "Value can not be null ".

IDataRepository repository = MockRepository.GenerateStub<IDataRepository>();
IKernel kernel = MockRepository.GenerateStub<IKernel>();
kernel.Expect(k => k.Get<IDataRepository>()).Return(repository);

Inline image 1
What should I do ?
Regards

Bill Richards

unread,
Dec 31, 2012, 11:28:41 AM12/31/12
to rhino...@googlegroups.com
You need to use Stub not Expect. 

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rhinomocks/-/r_ElcSl13asJ.
To post to this group, send email to rhino...@googlegroups.com.
To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en.

M

unread,
Jan 1, 2013, 5:18:56 PM1/1/13
to rhino...@googlegroups.com
Sorry, Didn't work. Still getting the same exception.

Iain Waddell

unread,
Jan 1, 2013, 5:43:07 PM1/1/13
to rhino...@googlegroups.com
I believe you also need to change Expect to Stub.

Iain

--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rhinomocks/-/t-M9lSERsE8J.

M

unread,
Jan 1, 2013, 7:10:00 PM1/1/13
to rhino...@googlegroups.com
But believe me, It is not working, It is just 3 lines of code, you can see it is not working with either Stub or Expect.

I think testing Ninject Kernels are painful with Rhino and that's why these project exists : https://github.com/ninject/ninject.mockingkernel

I used MOQ and it mocked the IKernel like a charm.

Cheers

Fabian Schmied

unread,
Jan 2, 2013, 4:16:42 AM1/2/13
to rhino...@googlegroups.com
Hi,
 
"Get" is an extenstion method; it cannot be mocked using RhinoMocks; only virtual methods can be mocked. The ArgumentNullException is caused by "Get" in turn invoking the virtual method "Resolve", which is caught by RhinoMocks and made to return "null". This is then passed to Enumerable.Cast, which throws the ArgumentNullException..
 
The correct way of solving this is by stubbing the virtual methods, i.e., "Resolve" (and before that, "CreateRequest"), rather than "Get".
 
(Moq, BTW, also cannot mock extension methods. However, Moq might return a recursive stub rather than null when "Get" calls "Resolve", which prevents the ArgumentNullException from being thrown. I don't think RhinoMocks can be configured to return recursive stubs rather than null results on method calls.)
 
Best regards,
Fabian
Reply all
Reply to author
Forward
0 new messages