Logging failed mappings
See original GitHub issueI’m developing an app that works with several other APIs, and the typical scenario is to record some interactions and use these records later as test data. Sometimes I need to edit a third-party response to a specific request, and I ofter run into trouble “no specific matching found”. I thought it would be ideal to save the data about failed matching to analyze it. How can I do it?
I use wiremock.net in tests like described here: https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests so my option is to use the last method from these tips (to access log entries via C# code): https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-Tips
I use it like this:
private readonly WireMockServer _authServer = create(...);
private static WireMockServer create(...)
{
var settings = new WireMockServerSettings
{
Urls = new[] { mockUri.AbsoluteUri },
FileSystemHandler = new MyLocalFileSystemHandler(...),
ReadStaticMappings = true,
};
return WireMockServer.Start(settings);
}
public void Stop()
{
logFailedRequests(_authServer);
_authServer.Stop();
}
private static void logFailedRequests(WireMockServer server)
{
foreach (ILogEntry entry in server.LogEntries.Where(e => !e.PartialMatchResult.IsPerfectMatch))
{
string ser = JsonConvert.SerializeObject(entry, Formatting.Indented);
File.WriteAllText(Path.Combine(..., entry.Guid + ".json"), ser);
}
}
The problem is that I have several mock servers, and even if I move logFailedRequests
to some utility class, it’s a bit clunky to repeat this call in every mock before stopping and disposing. Right now, the only thing I can think of is, is to build a facade around WireMockNet and do the logging there.
What can I do to simplify this? Maybe some configuration trick or overriding of something.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
OK. I’ll merge this PR and create a new official version in some time.
Thank you! Just checked, that worked, and the code was simplified significantly, well done! 👍