jeudi 20 février 2020

How to count function calls for unittesting?

I use my own small unittesting framework to test my code.

Sometimes I need the functionality to count how often a function of a fake object is called. Like this:

IDatabase fakeDb = new FakeDatabase();
IResult result = new Foo().ToTest(fakeDb);

//Some other tests

UTest.Equal(fakeDb.FakeCloseCalled(), 1);

So at the moment my FakeDatabase-class looks like this:

class FakeDatabase : IDatabase
{
    //All the other code

    public int FakeCloseCalled
    {
        get;
        private set;
    }

    public void Close()
    {
        FakeCloseCalled++;
    }
}

So, everytime I want to know how often a function of my fake objects was called I have to create a property for this in my FakeClass.

So I wonder if there is a more "generic" way to do this. e.g. profilers and some unittest-frameworks seem to be able to count the calls to functions.

So is there a more generic way to count the calls to Close without creating a property every time?





Aucun commentaire:

Enregistrer un commentaire