How do I mock an object based on an interface and set a read-only property?

I'm new to TDD. Therefore, any help would be greatly appreciated. I am using NUnit and Rhino. How do I set the ID value to 1 in my wet object?

I looked at this: http://www.iamnotmyself.com/2008/06/26/RhinoMocksAndReadOnlyPropertyInjectionPart2.aspx but reflection doesn't seem to work with interfaces.

    public interface IBatchInfo
    {
        int ID { get;}
        Branches Branch { get; set; }
        string Description { get; set; }                                
    }

 [SetUp]
       public void PerFixtureSetup()
       {

           _mocks = new MockRepository();
           _testRepository = _mocks.StrictMock<IOLERepository>();

       }

    [Test]
            public void ItemsAreReturned()
            {
                IBatchInfo aBatchItem=  _mocks.Stub<IBatchInfo>();

                aBatchItem.ID = 1; //fails because ID is a readonly property
                aBatchItem.Branch = Branches.Edinburgh;


                List<IBatchInfo> list = new List<IBatchInfo>();

                list.Add( aBatchItem);

                Expect.Call(_testRepository.BatchListActive()).Return(list);
                _mocks.ReplayAll();

                BatchList bf = new BatchList(_testRepository, "usercreated", (IDBUpdateNotifier)DBUpdateNotifier.Instance);
                List<Batch> listofBatch = bf.Items;

                Assert.AreEqual(1, listofBatch.Count);
                Assert.AreEqual(1, listofBatch[0].ID);
                Assert.AreEqual( Branches.Edinburgh,listofBatch[0].Branch);
            }

      

0


source to share


2 answers


Better yet, use rhino 3.5:



aBatch.Stub(x => x.ID).Return(0);

      

+1


source


Found the answer here http://haacked.com/archive/2007/05/04/setting-propertybehavior-on-all-properties-with-rhino-mocks.aspx .

Simple, instead of

aBatchItem.ID=1;

      



using:

SetupResult.For(aBatchItem.ID).Return(1);

      

+1


source







All Articles