Brian Colwell
Brian Colwell

Reputation: 79

How do I match an argument based on its base class type in rhino mocks?

I'm attempting to stub a method using a type constraint on one of the arguments. Normally I already know the type and write:

o.Stub(x => x.SomeMethod(Arg<bool>.Is.Anything)).Return(...);

Instead, I'd like to catch all calls to SomeMethod where the first arg passed in derives from a base class, say B. Is this possible? Can someone help with an example?

Pseudo code for what I'd like to specify:

o.Stub(x => x.SomeMethod(Arg.Type.Equals(typeof(B))).Return(...);

and have this catch calls like SomeMethod(a); where a is of type A and A derives from B.

Upvotes: 0

Views: 297

Answers (2)

Amittai Shapira
Amittai Shapira

Reputation: 3827

Rhino Mocks does support that as part of its constraints, your code should be something like:

o.Stub(x => x.SomeMethod(Arg<B>.Is.TypeOf)).Return(...);

Upvotes: 1

Nkosi
Nkosi

Reputation: 247068

Given your example.

o.Stub(x => x.SomeMethod(Arg<B>.Is.Anything)).Return(...);

...should work for any classes derived from B.

Upvotes: 0

Related Questions