8

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?

slang
  • 626
  • 7
  • 26
Daniel
  • 8,133
  • 5
  • 36
  • 51

3 Answers3

8

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();
MonkeyPushButton
  • 1,077
  • 10
  • 19
2

A cleaner way than would be:

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

I just don't get why GenerateStub doesn't work.

slang
  • 626
  • 7
  • 26
Daniel
  • 8,133
  • 5
  • 36
  • 51
1

I received this same message. My issue was that I was trying to stub a non-virtual property on a concrete class.

MoMo
  • 8,149
  • 3
  • 35
  • 31