BjRo/Machine.Fakes — Issue #44: Mocking a property with get and set using Rhino.Mocks
Author: Jobbe
Created: 2011-11-01 07:57:02+00:00
State: open

There is a problem when mocking a property with get and set. You will get following exception:

System.InvalidOperationException: You are trying to set an expectation on a property that was defined to use PropertyBehavior.
Instead of writing code such as this: mockObject.Stub(x => x.SomeProperty).Return(42);
You can use the property directly to achieve the same result: mockObject.SomeProperty = 42;

Here is the specification:

public class When_using_rhino_fake_engine_to_mock_a_property_with_get_and_set : WithFakes
{
Establish context = () =>
{
_subject = An();
};

Because of = () => _subject.WhenToldTo(a => a.Value).Return("abc");

It should_return_the_mocked_value = () => _subject.Value.ShouldEqual("abc");

static IFoo _subject;
}

public interface IFoo
{
string Value { get; set; }
}

With all other adapters it works fine...