Search code examples
c#.netunit-testingrhino-mocksstub

How can I set a value of a property without setter on a stub?


Can I set a return value of a setter-less property of a stub that is created by Rhino.Mocks?

For example:

public interface IMyMachine { string myProperty { get; } }

...

IMyMachine m = MockRepository.GenerateMock<IMyMachine>();

// implement in a way so that m.myProperty will return "Ahoj!"
if (m.myProperty == "Ahoj!")
 //do something

Solution

  • m.Expect(x => x.myProperty).Return("abc");
    

    or if you use a stub:

    var m = MockRepository.GenerateStub<IMyMachine>();
    m.Stub(x => x.myProperty).Return("abc");