2008-12-12 65 views
0

我是TDD的新手。所以任何幫助,將不勝感激。我正在使用NUnit和Rhino mock。 如何在我的模擬對象中將ID值設置爲1?如何基於接口創建模擬對象並設置只讀屬性?

我看過這個:http://www.iamnotmyself.com/2008/06/26/RhinoMocksAndReadOnlyPropertyInjectionPart2.aspx 但反射看起來不起作用對接口。

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); 
      } 

回答

1

更妙的是,如果使用犀牛嘲笑3.5:

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

謝謝你,這正是我一直在尋找! – mxmissile 2009-04-09 20:11:36