Closed srollinet closed 3 months ago
@srollinet
Could it also be an option to just create a helper method in WireMockAssertions.WithBody.cs
which formats all possible body values?
It could also probably be an option. I can check that next day if you want.
Sure. A PR is welcome.
Okay, doing something like this makes the message better
public AndConstraint<WireMockAssertions> WithBodyAsJson(IObjectMatcher matcher, string because = "", params object[] becauseArgs)
{
var (filter, condition) = BuildFilterAndCondition(r => r.BodyAsJson, matcher);
return ExecuteAssertionWithBodyAsIObjectMatcher(matcher, because, becauseArgs, condition, filter, r => FormatBody(r.BodyAsJson));
}
private static object? FormatBody(object? body)
{
if (body is JToken jToken)
{
return jToken.ToString(Formatting.None);
}
//TODO Add other types here
return body;
}
Expected wiremockserver to have been called using body { param = value2, otherParam = 18067588-16df-4f27-ab2b-a737206f97ff }, but didn't find it among the body {"{"param":"value","otherParam":42}"}.
But it is still different between the expected body and the actual body
And it is worse if the Matcher is created by using a named type, or even worse if the type overrides ToString()
Expected wiremockserver to have been called using body WireMock.Net.Tests.FluentAssertions.WireMockAssertionsTests+RequestInputDto { OtherParam = {718de8cd-21ef-4406-b631-1cd99c1a4328}, Param = "value" }, but didn't find it among the body {"{"Param":"value","OtherParam":"6d3380ff-bfcf-4269-8d81-01ac24d13645"}"}.
I wonder if the expected body should also be serialized as json, so both representations will be similar. What do you think?
@srollinet Can you take a look at my PR ?
@StefH Yes, it looks good to me
Describe the bug
When using a Json Body, Fluent assertion cannot format the actual body in the error message
Expected behavior:
The json body is correctly formatted
Test to reproduce
You can reproduce the message with this unit test (proper assertion is not done, the test fails with the unformatted message)
Other related info
One solution is to implement a custom
IValueFormatter
Example:
And to add it in the custom formatters
Formatter.AddFormatter(new JObjectFormatter());
Note: the
IValueFormatter
interface varies based on the target framework, so it must be multiple implementationsLet me know if I can help or if I can make a PR for this issue