开发者

Rhino Mocks, test that the method being tested does not call a given method

开发者 https://www.devze.com 2023-01-29 13:36 出处:网络
I have a class that I\'m testing, let\'s say: class Service{ public virtual DALClass DALAccess{get;set;}

I have a class that I'm testing, let's say:

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

I want to test the Save method and as part of my test I want that based on a property in TEntity assert that the method Validate is not called and that a method in the DALClass does.

This is what I have:

[TestMethod]
void TestSave(){
 //arrange
 TEntity entity = new TEntity();
 Service service = new Service();
 DALClass dal = MockRepository.GenerateMock<DALCla开发者_开发知识库ss >();
 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

Thanks, Ignacio


Create a PartialMock of Service. Then stub out the call to Validate and have it do an Assert.Fail when called. Something like this:

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


Create a new TEST_Service class that derives from Service, overrides .Validate and logs whether it's called or not:

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

Then check service.ValidateCalled

0

精彩评论

暂无评论...
验证码 换一张
取 消