2010-12-09 94 views
0

我有我的測試類,比方說:犀牛製品,測試被測試的方法不調用給定的方法

class Service{ 
public virtual DALClass DALAccess{get;set;} 
public virtual void Save(TEntity t){} 
public virtual bool Validate(TEntity t) 
} 

我想測試Save方法和部分我測試我希望基於TEntity中的屬性聲明方法Validate未被調用,並且DALClass中的一個方法可以。

這是我有:

[TestMethod] 
void TestSave(){ 
//arrange 
TEntity entity = new TEntity(); 
Service service = new Service(); 
DALClass dal = MockRepository.GenerateMock<DALClass >(); 
dal.Expect(t => t.MustSaveInstance(Arg.Is(entity))).Return(false); 
service.DALAccess = dal; 
//act 
service.Save(entity); 
dal.VerifyAllExpectations(); 

//Question, how can I verify that service.Validate is not called 

感謝, 伊格納西奧

回答

0

創建一個從服務派生的新TEST_Service類,覆蓋.Validate和日誌是否就是所謂與否:

class TEST_Service : Service 
{ 
    public override bool Validate(...) 
    { 
     // Remember that I was called 
     ValidateCalled = true; 
     base.Validate(...); 
    } 
    public bool ValidateCalled { get; set; } 
} 

然後檢查service.ValidateCalled

+0

如果驗證不是虛擬的呢? – ignacio 2010-12-09 22:08:50

1

創建服務的PartialMock。然後將調用存入驗證,並在調用時執行Assert.Fail。類似這樣的:

service.Stub(s => s.Validate(entity)).WhenCalled(i => Assert.Fail("Validate called"));