Generic methods and AssertWasCalled

205 views
Skip to first unread message

Steinkauz

unread,
Oct 25, 2012, 5:30:26 AM10/25/12
to rhino...@googlegroups.com
So i have a mocked object which has generic method
public virtual void SendMessage<T>(T reply) where T : IRequest

I have multiple classes which implement all IRequest.

In code i am testing method gets called multiple times like:
switch(type)
{
case Class1:
{
SendMessage(new Class1() );
break;
}

case Class2:
{
SendMessage(new Class2() );
break;
}
case Class3:
{
SendMessage(new Class3() );
break;
}

//and so on...
}
I want to make sure NONE of this methods is called.

But if i make m_provider.AssertWasNotCalled(x => x.SendMessage(Arg<IRequest>.Is.Anything), o => o.IgnoreArguments());

This doesnt assert right.

Because if i call SendMessage(new Class1()) and then
m_provider.AssertWasCalled(x => x.SendMessage(Arg<IRequest>.Is.Anything), o => o.IgnoreArguments());
this failes but this
m_provider.AssertWasCalled(x => x.SendMessage(Arg<Class1>.Is.Anything>()), o => o.IgnoreArguments());
passes

IgnoreArguments here is overkill, i know

So how do you assert none of the generic method instances was called?

Sunny

unread,
Oct 29, 2012, 10:04:23 AM10/29/12
to rhino...@googlegroups.com
You're better of if you split your implementation> Have a
IRequestCreator factory class, which creates the instances of Class1,
class2, etc.

Your SendMessage method will have this non-generic signature:

public void SendMessage(IRequest reply);

Then, your previous code will look like:

var myRequest = requestCreator.Create(typeof(xxxx));
if (myRequest != null)
{
SendMessage(myRequest);
}

Now, you can mock requestCreator, make it return null on certain
condition, and test that SendMessage was not called.

And another group of tests, to test the actual creator, and make sure
it returns actual instance or null, when needed.

Cheers
> --
> 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/-/x-12icZMwnYJ.
> 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.



--
Svetoslav Milenov (Sunny)

Artificial Intelligence is no match for natural stupidity.

haifisch

unread,
Nov 13, 2012, 3:13:20 PM11/13/12
to rhino...@googlegroups.com
Hi Steinkauz,

regarding the design of you code sunny is definitely right - despite that I've tried to improve the behavior of rhino.mocks when handling generic methods (https://github.com/alaendle/rhino-mocks/commit/850b18d10c30217c47a8b5ef92927bab3f5d5c3f). If you like you could take a look - I'm aware that my changes might not working 100% for some corner cases - but all the unit tests are passes - and the handling of generic functions is not worse than before ;-)

Br,

Andreas

Steinkauz

unread,
Nov 14, 2012, 8:23:13 AM11/14/12
to rhino...@googlegroups.com
Hi.
Unfortunately i have little influence on the design of the code(and the project is mostly done).

Its great that you took your time and tried to fix the problem.
I will give it a try, but do you know when can we expect this fix in new release?

Because adding build RhinoMock.dll to project and referencing is an option but VisualStudio nuget package manager ->RhinoMock is much more dynamic solution and there is no need for manual updates in the future.

Dne četrtek, 25. oktober 2012 11:30:26 UTC+2 je oseba Steinkauz napisala:
Reply all
Reply to author
Forward
0 new messages