Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks problems with private setter in stub

Error:

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;

var x = MockRepository.GenerateStub<MyClass>();
x.Stub(s => s.Items).Return(new List<Item>());

public class MyClass
{
    public virtual IEnumerable<Item> Items
    {
        get {return _items;}
        private set {_items = value;}
    }
}

What am I doing wrong?

like image 783
Daniel Avatar asked Apr 27 '11 15:04

Daniel


1 Answers

I think using a Mock rather than a stub gets around the problem, but there may be a better way I'm missing.

        var x = MockRepository.GenerateMock<MyClass>();
        x.BackToRecord(BackToRecordOptions.PropertyBehavior);
        SetupResult.For(x.Items).Return(new List<Item>());
        x.Replay();
like image 178
MonkeyPushButton Avatar answered Oct 01 '22 04:10

MonkeyPushButton