ignacio
ignacio

Reputation: 1

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

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<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

Thanks, Ignacio

Upvotes: 0

Views: 733

Answers (2)

PatrickSteele
PatrickSteele

Reputation: 14677

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

Upvotes: 1

n8wrl
n8wrl

Reputation: 19765

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

Upvotes: 0

Related Questions