2008-12-12 8 views
0

Je suis nouveau à TDD. Donc toute aide serait appréciée. J'utilise les simulacres NUnit et Rhino. Comment puis-je définir la valeur d'ID à 1 dans mon objet maquette?Comment créer un objet maquette basé sur une interface et définir une propriété en lecture seule?

J'ai regardé ceci: http://www.iamnotmyself.com/2008/06/26/RhinoMocksAndReadOnlyPropertyInjectionPart2.aspx mais la réflexion ne semble pas fonctionner contre les 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); 
      } 

Répondre

1

si vous utilisez Rhino se moque 3,5:

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

Merci, c'était juste ce que je cherchais! – mxmissile

Questions connexes