Expect and overloaded methods

202 views
Skip to first unread message

Al

unread,
Jul 6, 2009, 9:29:46 PM7/6/09
to Rhino.Mocks
Hi,

I have a question you might help me with.
We need to verify that a method A calls any of the overloaded forms of
method B.

e.g. I have
void B(int)
void B(int, int)
void B(int, int, int)

and I don't care which one of those is called from my method A (at
least one must be called).
Is it possible with Rhino Mocks to define "alternative
expectations" ? Like a set of expectation from which at least one
must be met (or something like .VerifyAnyExpectation instead
of .VerifyAllExpectations) ?

If this is not possible, what would you suggest for this scenario?

What I did now is grouped the 3 methods in the most complex one.
The call B(x) will now be B(x, null, null), but I don't like this work-
around and I'm looking for better alternatives.

Thanks,
Al



Tim Barcz

unread,
Jul 7, 2009, 12:52:18 PM7/7/09
to Rhino...@googlegroups.com
What is the usage of this....?

The code isn't random as runtime is it?  There is logic in place to determine what actual method should be called.  If that is the case you can assert the different scenarios.

Tim
--
Tim Barcz
ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

Kenneth Xu

unread,
Jul 7, 2009, 2:10:16 PM7/7/09
to Rhino...@googlegroups.com
I guess this is some sort of state v.s. interactive test. I had the
same need for this.

Yes, there is a logic to determine which is called but this is
implementation detail. If my email service as overloaded sendmail
methods. All I care is one of them is called. Which one is called is
the implementation detail and I don't want my test case to fail if the
implementation changes in future.

There is no direct support from RhinoMocks for this but you can do
below to archive the same result:

try {
mock.AssertWasCalled(x=>x.B(i));
} catch (FogotNameButYouCanFindOutEasilyException)
{
try {
mock.AssertWasCalled(x=>x.B(i,j));
} catch (...) {
mock.AssertWasCalled(x=>x.B(i,j,k));

Al

unread,
Jul 8, 2009, 12:36:37 AM7/8/09
to Rhino.Mocks
Thanks Kenneth,

What you described is exactly what I have. Sorry I didn't explain
better myself.

I just wanted to check with you that Rhino Mocks doesn't provide
support for that, before trying something else.

I'll do something similar with what you proposed and make sure my
condition (that exactly one of the function is called) is met.

Cheers,
Al

On Jul 8, 4:10 am, Kenneth Xu <kenne...@gmail.com> wrote:
> I guess this is some sort of state v.s. interactive test. I had the
> same need for this.
>
> Yes, there is a logic to determine which is called but this is
> implementation detail. If my email service as overloaded sendmail
> methods. All I care is one of them is called. Which one is called is
> the implementation detail and I don't want my test case to fail if the
> implementation changes in future.
>
> There is no direct support from RhinoMocks for this but you can do
> below to archive the same result:
>
> try {
>   mock.AssertWasCalled(x=>x.B(i));} catch (FogotNameButYouCanFindOutEasilyException)
>
> {
>   try {
>     mock.AssertWasCalled(x=>x.B(i,j));
>   } catch (...) {
>     mock.AssertWasCalled(x=>x.B(i,j,k));
>   }
>
>
>
> }
> On Tue, Jul 7, 2009 at 12:52 PM, Tim Barcz<timba...@gmail.com> wrote:
> > What is the usage of this....?
>
> > The code isn't random as runtime is it?  There is logic in place to
> > determine what actual method should be called.  If that is the case you can
> > assert the different scenarios.
>
> > Tim
>
> >http://www.twitter.com/timbarcz- Hide quoted text -
>
> - Show quoted text -

Kenneth Xu

unread,
Jul 8, 2009, 10:48:08 PM7/8/09
to Rhino...@googlegroups.com
Al,

You are welcome. I understood your pain of writing the test as I have
experienced the same. Your request confirmed that I'm not the only one
needed this feature so I went ahead and tried something myself.

How does this look to you?

(
mock.ActivityOf(m=>m.B(1)) |
mock.ActivityOf(m=>m.B(1,2)) |
mock.ActivityOf(m=>m.B(1,2,3))
).AssertOccured;

If you want exactly once, you can do:

Activities.OneOf(
foo.ActivityOf(f=>f.Foo(1), m=>m.Repeat.Once()),
foo.ActivityOf(f=>f.Foo(1,2), m=>m.Repeat.Once()),
foo.ActivityOf(f=>f.Foo(1,2,3), m=>m.Repeat.Once())
).AssertOccured;

If you are interested and want to give it a try. See
http://kennethxu.blogspot.com/2009/07/introduce-powerful-aaa-syntax-for.html
for detail.

Cheers,
Kenneth

Al

unread,
Jul 9, 2009, 1:38:32 AM7/9/09
to Rhino.Mocks
Hey Kenneth,

That's awsome.
I'm reading on your blog to see exactly what you did there.
But this is the functionality I want.

Thanks and keep doing the good work :)

Al
Reply all
Reply to author
Forward
0 new messages