Thursday, 19 April 2012

Rhino difference between .Is.Anything and .Is.Typeof

Rhino mocks, when matching arguments, it is preferential, as usual, to be as specific as you can about expected arguments (including repeat information). For instance, if you will call a method with e.g. a username, then set the expectation to expect that specific username:

Mock.Expect(r => r.SetUsername(Arg.Is.Equal("expectedusername"))).Repeat.Once()

There is an "Is" constraint called Anything. You might expect the following to match any string:

Mock.Expect(r => r.SetUsername(Arg.Is.Anything)).Repeat.Once()

But it doesn't! It matches anything! This is slightly misleading since the Arg class is parameterised and would seem a bit redundant but it is still the way it is. What you should do to achieve this functionality is to use TypeOf instead:

Mock.Expect(r => r.SetUsername(Arg.Is.TypeOf)).Repeat.Once()
Post a Comment