tags:

views:

535

answers:

1

I am setting up an expectation for a call to a method that builds and executes a query. I would like to interrogate the properties of the parameter used. Is this possible

using (mocks.Record())
{
    Expect.Call(connection.Retrieve(SOMETHING_HERE)).Return(returnedDatay);
}

The bit I am after is the "SOMETHING HERE" bit.

(This is my first time using Rhino mocks)

+2  A: 

You can set up constraints on your parameters and on the properties of the parameters. The following code sets up a constraint on a property named MyProperty on your connection object. The mock expects the MyProperty to be 42. Notice, that null is passed as the parameter since it is ignored.

Expect
    .Call(connection.Retrieve(null))
    .IgnoreArguments()
    .Constraints(Property.Value("MyProperty", 42))
    .Return(returnedData);

I am writing this from memory so it may not be absolutely correct.


UPDATE:

Rhino Mocks version 3.5 introduces a new extension method GetArgumentsForCallsMadeOn that lets you inspect the parameters passed to the mocked objects:

http://kashfarooq.wordpress.com/2009/01/10/rhino-mocks-and-getargumentsforcallsmadeon/

Jakob Christensen
I can't seem to get what I need from that. Where you have put null, that is the parameter I want to see.
Why do you want to interrogate the properties of the parameter? Is it because you want to verify what the properties are? Because that is exactly what my code does.
Jakob Christensen
Just found out that version 3.5 of Rhino Mocks has a new extension method GetArgumentsForCallsMadeOn that lets you inspect parameters passed to your mocked objects.
Jakob Christensen
It's fine to use null with IgnoreArguments and Constraints. The constraints are asserting on the arguments.
Matt Hinze