我正在测试一个课程,让我们说:
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 :(得分:1)
创建PartialMock of Service。然后将对Validate的调用存根,并让它在调用时执行Assert.Fail。像这样:
service.Stub(s => s.Validate(entity)).WhenCalled(i => Assert.Fail("Validate called"));
答案 1 :(得分:0)
创建一个派生自Service的新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