tags:

views:

299

answers:

1

Hi,

I'm trying to use the fluent mocking style of Rhino.Mocks and have the following code that works on a mock IDictionary object called 'factories':

With.Mocks(_Repository).Expecting(() =>
        {
            Expect.Call(() => factories.ContainsKey(Arg<String>.Is.Anything));
            LastCall.Return(false);

            Expect.Call(() => factories.Add(Arg<String>.Is.Anything, Arg<Object>.Is.Anything));
        }).Verify(() =>
        {
            _Service = new ObjectRequestService(factories);
            _Service.RegisterObjectFactory(Valid_Factory_Key, factory);
        });

Now, the only way I have been able to set the return value of the ContainsKey call is to use LastCall.Return(true) on the following line.

I'm sure I'm mixing styles here as Expect.Call() has a .Return(Expect.Action) method but I can't figure out how I am suppose to use it correctly to return a boolean value?

Can anyone help out? Hope the question is clear enough - let me know if anyone needs more info!

Cheers, Ben

+2  A: 

I believe the following line will give you want you want:

Expect.Call(factories.ContainsKey(Arg<string>.Is.Anything)).Return(false);
Jakob Christensen
Hi Jakob,Thanks for your reply! Yes I could modify my test slightly to remove the lambda expression and that does allow me to specify a boolean return value!I'm still curious to know how the .Return(Expect.Action) method is suppose to work though, especially since I'm using Lambda throughout my tests! It looks like I should be able to specify a return value and use a lambda, unless I am completely misunderstanding something...
Ben Cawley
Hi Ben, You should only use a delegate/lambda in Expect.Call when the mocked function returns void. It does not make sense to use the lamda expression and .Return(Expect.Action) together. The .Return(Expect.Action) appears because the lambda expression is of type Expect.Action in the same way the .Return(bool) appears when using the syntax I used for Expect.Call.
Jakob Christensen
Ah ok, that makes more sense! Thanks for your help Jakob.
Ben Cawley