How to record and replay method calls in C # .net to test legacy code isolation

I have a repository and a consumer of that repository in some legacy code.

The user makes multiple calls to methods on the repository.

Each method call returns a huge set of results.

I have an integration test that checks how the consumer and the repository behave together, but this is not good for me - it relies on the database connection, very slow, and doesn't help me know if the test is failing due to changes in a repository, database, or consumer.

I would like to transform this integration test to test the consumer separately - regardless of the repository implementation.

But since this is legacy code, the behavior of which I have not fully understood yet, and because the set of results is huge, I cannot manually stub the repository. If it could be written by hand, it would look like

var mockRepository = new Mock<IRepository>();
mockRepository.SetUp(r => r.GetCustomersInMarket(marketId: 3))
    .Returns(
        new List<Customer> {
            new Customer {
                ...
                },
            new Customer {
                ...
                },
            ... x 100   // large dataset
            });
mockRepository.SetUp(r => r.GetProductsInMarket(marketId: 3))
    .Returns(
         ...
     );
... x 15   // many different calls need to be set up


var testee = new Consumer(mockRepository.Object); // using dependency injection

var report = testee.GenerateReport();

AssertReportMatchesExpectations(report);  // some assertions

      

So what I'd rather do (one time)

var recordingProxy = new RecordingProxy<IRepository>(new Repository());

var testee = new Consumer(recordingProxy);

var report = testee.GenerateReport();

var methodCallLog = recordingProxy.GetLog();

      

and after that

var methodCallLog = GetStoredMethodCallLog(); // load persisted log from file or similar

var replayingProxy = new ReplayingProxy<IRepository>(methodCallLog);

var testee = new Consumer(replayingProxy);

var report = testee.GenerateReport();

AssertReportMatchesExpectations(report);  // some assertions

      

I started working on a generic tool to act as a proxy and also record and replay traffic that traverses the interface to solve this problem in general.

Is there anything like this?

Are there other ways to solve the stubbing repos issue

  • to return large datasets
  • when the content of the dataset can only be understood by observing the existing behavior (since the intent of the author is not clear).

If not, I'll post my tool as an answer to this question.

+3


source to share





All Articles