If you want to mock properties, you need to keep in mind that properties are merely special syntax for normal methods, a get property will translate directly to propertyType get_PropertyName() and a set property will translate directory to void set_PropertyName(propertyType value). So how do you create expectations for a property? Exactly as you would for those methods.
Here is how you set the return value for a get property:
IList list = mocks.CreateType<IList>();
SetupResult.For(list.Count).Return(42);
And here is how you would set an expectation for a set property:
IList list = mocks.CreateType<IList>();
list.Capacity = 500;//Will create an expectation for this call
LastCall.IgnoreArguments();//Ignore the amount that is passed.
One interesting feature you can take advantage of is automatic handling of properties. You use it like this:
[Test]
public void PropertyBehaviorForSingleProperty()
{
Expect.Call(demo.Prop).PropertyBehavior();
mocks.ReplayAll();
for (int i = 0; i < 49; i++)
{
demo.Prop = "ayende" + i;
Assert.AreEqual("ayende" + i, demo.Prop);
}
mocks.VerifyAll();
}
As you can see, you specify PropertyBehavior() on the property, and that is all. Rhino Mocks will emulate a simple property behavior for the property. You can see that this is not a transient behavior, but one that remains for the lifetime of the object.
Up:
Rhino Mocks Documentation
Next:
Rhino Mocks Callbacks