Stubbing a Property get using Rhino Mocks

Viewed 23790

Using RhinoMocks, I am trying to Stub the getter value of a property. The property is defined as part of a Interface with only getter access.

However I get the error "Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method)." I understand this may mean that the property I am stubbing is not virtual; However it is part of the Interface and I am not sure if that is the reason why I get this error..

Below is the code skeleton. If I uncomment the line which says "stubRepository.Stub(x => x.StoreDeviceID).PropertyBehavior();", then I get a new error "Property must be read/write". I searched on SO and found this page. But the proposed solution doesn't help me. Any thoughts?

public interface IStore {
        string StoreDeviceID {get;}
        //other methods
    }

    public static class Store {
        private IStore Repository;

        public void SetRepository(IStore rep){
            Repository = rep;
        }

        public StoredeviceID {
            get{
                return Repository.StoreDeviceID;
            }
        }

        //other methods
    }

    public class TestClass {
        [Test]
        public void TestDeviceID() {
            var stubRepository =
                MockRepository.GenerateStub<IStore>();
            Store.SetRepository(stubRepository);

            //stubRepository.Stub(x => x.StoreDeviceID).PropertyBehavior();
            SetupResult.For(stubRepository.StoreDeviceID).Return("test");

            Assert.AreSame(Store.StoreDeviceID, "test");
        }
    }
2 Answers
Related