richardszalay/mockhttp

Feature request: clear match counts

Closed this issue · 4 comments

I would like to be able to reset the counts of executed requests, without having to recreate all handlers.

I have this in my code, but it is not maintainable:

(helper.HttpMock is an instance of MockHttpMessageHandler)

        var field = helper.HttpMock.GetType().GetField("matchCounts", BindingFlags.Instance | BindingFlags.NonPublic);
        if (field != null)
        {
            var matchCounts = (Dictionary<IMockedRequest, int>) field.GetValue(helper.HttpMock);
            matchCounts.Clear();
        }

I'd be happy to accept a pr for this (just make sure to include unit tests)

Though to clarify, I wouldn't want this particular field to be made public. Do you want access to match counts or to reset them?

Apologies for the late reply. I had tried to upgrade to RestSharp v107 and this required mocking the HTTP message handler.
This upgrade was cancelled (too much to re-test) so I stopped using the mock.

I had copied/pasted MockHttpMessageHandler in my test project, where I added:
public void ClearInvocations()
{
lock (_lockObject)
{
OutputHelper.WriteLine($"{DateTime.Now:HH:mm:ss:ffff} Clearing invocations on instance {InstanceId}");
_matchCounts.Clear();
}
}

I'm going to close this issue. But if anyone else feels strongly about this feature, feel free to submit a PR!