Skip to content

Commit 6f11cbf

Browse files
committed
Fix FluentAssertions (actual body is not displayed in error message)
1 parent 27a6739 commit 6f11cbf

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.WithBody.cs

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma warning disable CS1591
22
using System;
33
using System.Collections.Generic;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Linq;
46
using WireMock.Matchers;
57

68
// ReSharper disable once CheckNamespace
@@ -9,7 +11,7 @@ namespace WireMock.FluentAssertions;
911
public partial class WireMockAssertions
1012
{
1113
private const string MessageFormatNoCalls = "Expected {context:wiremockserver} to have been called using body {0}{reason}, but no calls were made.";
12-
private const string MessageFormat = "Expected {context:wiremockserver} to have been called using body {0}{reason}, but didn't find it among the body {1}.";
14+
private const string MessageFormat = "Expected {context:wiremockserver} to have been called using body {0}{reason}, but didn't find it among the body/bodies {1}.";
1315

1416
[CustomAssertion]
1517
public AndConstraint<WireMockAssertions> WithBody(string body, string because = "", params object[] becauseArgs)
@@ -104,14 +106,14 @@ private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyAsIObjectMatch
104106
.ForCondition(requests => CallsCount == 0 || requests.Any())
105107
.FailWith(
106108
MessageFormatNoCalls,
107-
matcher.Value
109+
FormatBody(matcher.Value)
108110
)
109111
.Then
110112
.ForCondition(condition)
111113
.FailWith(
112114
MessageFormat,
113-
_ => matcher.Value,
114-
requests => requests.Select(expression)
115+
_ => FormatBody(matcher.Value),
116+
requests => FormatBodies(requests.Select(expression))
115117
);
116118

117119
FilterRequestMessages(filter);
@@ -148,4 +150,20 @@ private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyAsBytesExactOb
148150

149151
return new AndConstraint<WireMockAssertions>(this);
150152
}
153+
154+
private static string? FormatBody(object? body)
155+
{
156+
return body switch
157+
{
158+
null => null,
159+
JObject jObject => jObject.ToString(Formatting.None),
160+
JToken jToken => jToken.ToString(Formatting.None),
161+
_ => JToken.FromObject(body).ToString(Formatting.None)
162+
};
163+
}
164+
165+
private static string? FormatBodies(IEnumerable<object?> bodies)
166+
{
167+
return string.Join(", ", bodies.Select(FormatBody));
168+
}
151169
}

test/WireMock.Net.Tests/FluentAssertions/WireMockAssertionsTests.cs

+40-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,11 @@ public async Task HaveReceived1Call_WithBodyAsJson()
702702
// Act
703703
var httpClient = new HttpClient();
704704

705-
await httpClient.PostAsync($"{server.Url}/a", new StringContent(@"{ ""x"": ""y"" }"));
705+
var requestBody = new
706+
{
707+
x = "y"
708+
};
709+
await httpClient.PostAsJsonAsync($"{server.Url}/a", requestBody);
706710

707711
// Assert
708712
server
@@ -740,6 +744,41 @@ public async Task HaveReceived1Call_WithBodyAsJson()
740744
server.Stop();
741745
}
742746

747+
[Fact]
748+
public async Task WithBodyAsJson_When_NoMatch_ShouldHaveCorrectErrorMessage()
749+
{
750+
// Arrange
751+
var server = WireMockServer.Start();
752+
753+
server
754+
.Given(Request.Create().WithPath("/a").UsingPost().WithBodyAsJson(new { x = "y" }))
755+
.RespondWith(Response.Create().WithBody("A response"));
756+
757+
// Act
758+
var httpClient = new HttpClient();
759+
760+
var requestBody = new
761+
{
762+
x = "123"
763+
};
764+
await httpClient.PostAsJsonAsync($"{server.Url}/a", requestBody);
765+
766+
// Assert
767+
Action act = () => server
768+
.Should()
769+
.HaveReceived(1)
770+
.Calls()
771+
.WithBodyAsJson(new { x = "y" })
772+
.And
773+
.UsingPost();
774+
775+
act.Should()
776+
.Throw<Exception>()
777+
.WithMessage("Expected wiremockserver to have been called using body \"{\"x\":\"y\"}\", but didn't find it among the body/bodies \"{\"x\":\"123\"}\".");
778+
779+
server.Stop();
780+
}
781+
743782
[Fact]
744783
public async Task HaveReceived1Call_WithBodyAsBytes()
745784
{

0 commit comments

Comments
 (0)