Skip to content

Commit 9efd773

Browse files
committed
Allow assertions on requests that matched
1 parent eeff02f commit 9efd773

File tree

6 files changed

+285
-106
lines changed

6 files changed

+285
-106
lines changed

Mockly.ApiVerificationTests/ApprovedApi/FluentAssertions.Mockly.v7.net472.verified.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ namespace Mockly
1010
}
1111
public class ContainedRequestAssertions : FluentAssertions.Primitives.ReferenceTypeAssertions<Mockly.CapturedRequest, Mockly.ContainedRequestAssertions>
1212
{
13-
public ContainedRequestAssertions(Mockly.CapturedRequest request) { }
1413
protected override string Identifier { get; }
15-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBody(string wildcard, string because = "", params object[] becauseArgs) { }
16-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyEquivalentTo<T>(T expected, string because = "", params object[] becauseArgs) { }
17-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyHavingPropertiesOf(System.Collections.Generic.IDictionary<string, string> expectation, string because = "", params object[] becauseArgs) { }
18-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyHavingProperty(string key, string value, string because = "", params object[] becauseArgs) { }
19-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyMatchingJson(string json, string because = "", params object[] becauseArgs) { }
14+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBody(string wildcard, string because = "", params object[] becauseArgs) { }
15+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyEquivalentTo<T>(T expected, string because = "", params object[] becauseArgs) { }
16+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyHavingPropertiesOf(System.Collections.Generic.IDictionary<string, string> expectation, string because = "", params object[] becauseArgs) { }
17+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyHavingProperty(string key, string value, string because = "", params object[] becauseArgs) { }
18+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyMatchingJson(string json, string because = "", params object[] becauseArgs) { }
2019
}
2120
public static class HttpMockAssertionExtensions
2221
{

Mockly.ApiVerificationTests/ApprovedApi/FluentAssertions.Mockly.v7.net8.0.verified.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ namespace Mockly
1010
}
1111
public class ContainedRequestAssertions : FluentAssertions.Primitives.ReferenceTypeAssertions<Mockly.CapturedRequest, Mockly.ContainedRequestAssertions>
1212
{
13-
public ContainedRequestAssertions(Mockly.CapturedRequest request) { }
1413
protected override string Identifier { get; }
15-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBody(string wildcard, string because = "", params object[] becauseArgs) { }
16-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyEquivalentTo<T>(T expected, string because = "", params object[] becauseArgs) { }
17-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyHavingPropertiesOf(System.Collections.Generic.IDictionary<string, string> expectation, string because = "", params object[] becauseArgs) { }
18-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyHavingProperty(string key, string value, string because = "", params object[] becauseArgs) { }
19-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyMatchingJson(string json, string because = "", params object[] becauseArgs) { }
14+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBody(string wildcard, string because = "", params object[] becauseArgs) { }
15+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyEquivalentTo<T>(T expected, string because = "", params object[] becauseArgs) { }
16+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyHavingPropertiesOf(System.Collections.Generic.IDictionary<string, string> expectation, string because = "", params object[] becauseArgs) { }
17+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyHavingProperty(string key, string value, string because = "", params object[] becauseArgs) { }
18+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyMatchingJson(string json, string because = "", params object[] becauseArgs) { }
2019
}
2120
public static class HttpMockAssertionExtensions
2221
{

Mockly.ApiVerificationTests/ApprovedApi/FluentAssertions.Mockly.v8.net472.verified.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ namespace Mockly
1010
}
1111
public class ContainedRequestAssertions : FluentAssertions.Primitives.ReferenceTypeAssertions<Mockly.CapturedRequest, Mockly.ContainedRequestAssertions>
1212
{
13-
public ContainedRequestAssertions(Mockly.CapturedRequest request) { }
1413
protected override string Identifier { get; }
15-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBody(string wildcard, string because = "", params object[] becauseArgs) { }
16-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyEquivalentTo<T>(T expected, string because = "", params object[] becauseArgs) { }
17-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyHavingPropertiesOf(System.Collections.Generic.IDictionary<string, string> expectation, string because = "", params object[] becauseArgs) { }
18-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyHavingProperty(string key, string value, string because = "", params object[] becauseArgs) { }
19-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyMatchingJson(string json, string because = "", params object[] becauseArgs) { }
14+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBody(string wildcard, string because = "", params object[] becauseArgs) { }
15+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyEquivalentTo<T>(T expected, string because = "", params object[] becauseArgs) { }
16+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyHavingPropertiesOf(System.Collections.Generic.IDictionary<string, string> expectation, string because = "", params object[] becauseArgs) { }
17+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyHavingProperty(string key, string value, string because = "", params object[] becauseArgs) { }
18+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyMatchingJson(string json, string because = "", params object[] becauseArgs) { }
2019
}
2120
public static class HttpMockAssertionExtensions
2221
{

Mockly.ApiVerificationTests/ApprovedApi/FluentAssertions.Mockly.v8.net8.0.verified.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ namespace Mockly
1010
}
1111
public class ContainedRequestAssertions : FluentAssertions.Primitives.ReferenceTypeAssertions<Mockly.CapturedRequest, Mockly.ContainedRequestAssertions>
1212
{
13-
public ContainedRequestAssertions(Mockly.CapturedRequest request) { }
1413
protected override string Identifier { get; }
15-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBody(string wildcard, string because = "", params object[] becauseArgs) { }
16-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyEquivalentTo<T>(T expected, string because = "", params object[] becauseArgs) { }
17-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyHavingPropertiesOf(System.Collections.Generic.IDictionary<string, string> expectation, string because = "", params object[] becauseArgs) { }
18-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyHavingProperty(string key, string value, string because = "", params object[] becauseArgs) { }
19-
public FluentAssertions.AndConstraint<Mockly.ContainedRequestAssertions> WithBodyMatchingJson(string json, string because = "", params object[] becauseArgs) { }
14+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBody(string wildcard, string because = "", params object[] becauseArgs) { }
15+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyEquivalentTo<T>(T expected, string because = "", params object[] becauseArgs) { }
16+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyHavingPropertiesOf(System.Collections.Generic.IDictionary<string, string> expectation, string because = "", params object[] becauseArgs) { }
17+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyHavingProperty(string key, string value, string because = "", params object[] becauseArgs) { }
18+
public FluentAssertions.AndWhichConstraint<Mockly.ContainedRequestAssertions, Mockly.CapturedRequest> WithBodyMatchingJson(string json, string because = "", params object[] becauseArgs) { }
2019
}
2120
public static class HttpMockAssertionExtensions
2221
{

Mockly.Specs/AssertionSpecs.cs

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,23 @@ public async Task Matches_body_with_wildcard_pattern()
323323
.WithBody("*world");
324324
}
325325

326+
[Fact]
327+
public async Task Can_match_multiple_requests()
328+
{
329+
// Arrange
330+
var mock = new HttpMock();
331+
mock.ForPost().WithPath("/api/test").RespondsWithStatus(HttpStatusCode.Created);
332+
var client = mock.GetClient();
333+
334+
// Act
335+
await client.PostAsync("https://localhost/api/test", new StringContent("hello world"));
336+
await client.PostAsync("https://localhost/api/test", new StringContent("hallo wereld"));
337+
338+
// Assert
339+
mock.Requests.Should().ContainRequest().WithBody("*wereld*");
340+
mock.Requests.Should().ContainRequest().WithBody("*world*");
341+
}
342+
326343
[Fact]
327344
public async Task Fails_when_body_does_not_match_wildcard_pattern()
328345
{
@@ -361,6 +378,23 @@ public async Task Matches_body_equivalent_json()
361378
.WithBodyMatchingJson("{ \"id\": 1, \"name\": \"x\" }");
362379
}
363380

381+
[Fact]
382+
public async Task Can_match_against_multiple_requests()
383+
{
384+
// Arrange
385+
var mock = new HttpMock();
386+
mock.ForPost().WithPath("/api/test").RespondsWithStatus(HttpStatusCode.Created);
387+
var client = mock.GetClient();
388+
389+
// Act
390+
await client.PostAsync("https://localhost/api/test", new StringContent("{\n \"id\":1, \"name\":\"x\"\n}"));
391+
await client.PostAsync("https://localhost/api/test", new StringContent("{\n \"id\":2, \"name\":\"y\"\n}"));
392+
393+
// Assert
394+
mock.Requests.Should().ContainRequest().WithBodyMatchingJson("{ \"id\": 2, \"name\": \"y\" }");
395+
mock.Requests.Should().ContainRequest().WithBodyMatchingJson("{ \"id\": 1, \"name\": \"x\" }");
396+
}
397+
364398
[Fact]
365399
public async Task Fails_when_body_json_does_not_match()
366400
{
@@ -409,17 +443,21 @@ public async Task Matches_body_equivalent_to_anonymous_object()
409443
var client = mock.GetClient();
410444

411445
// Act
446+
await client.PostAsync("https://localhost/api/test", new StringContent("{ \"id\":2, \"name\":\"y\" }"));
412447
await client.PostAsync("https://localhost/api/test", new StringContent("{ \"id\":1, \"name\":\"x\" }"));
413448

414-
var expected = new
449+
// Assert
450+
mock.Requests.Should().ContainRequest().WithBodyEquivalentTo(new
415451
{
416452
id = 1,
417453
name = "x"
418-
};
454+
});
419455

420-
// Assert
421-
mock.Requests.Should().ContainRequest()
422-
.WithBodyEquivalentTo(expected);
456+
mock.Requests.Should().ContainRequest().WithBodyEquivalentTo(new
457+
{
458+
id = 2,
459+
name = "y"
460+
});
423461
}
424462

425463
[Fact]
@@ -458,17 +496,21 @@ public async Task Matches_body_having_properties_of_dictionary()
458496
var client = mock.GetClient();
459497

460498
// Act
499+
await client.PostAsync("https://localhost/api/test", new StringContent("{ \"id\":\"2\", \"name\":\"y\" }"));
461500
await client.PostAsync("https://localhost/api/test", new StringContent("{ \"id\":\"1\", \"name\":\"x\" }"));
462501

463-
var expected = new Dictionary<string, string>(StringComparer.Ordinal)
502+
// Assert
503+
mock.Requests.Should().ContainRequest().WithBodyHavingPropertiesOf(new Dictionary<string, string>(StringComparer.Ordinal)
464504
{
465505
["id"] = "1",
466506
["name"] = "x"
467-
};
507+
});
468508

469-
// Assert
470-
mock.Requests.Should().ContainRequest()
471-
.WithBodyHavingPropertiesOf(expected);
509+
mock.Requests.Should().ContainRequest().WithBodyHavingPropertiesOf(new Dictionary<string, string>(StringComparer.Ordinal)
510+
{
511+
["id"] = "2",
512+
["name"] = "y"
513+
});
472514
}
473515

474516
[Fact]
@@ -503,10 +545,9 @@ public async Task Fails_when_body_does_not_have_the_expected_property_and_value(
503545
var client = mock.GetClient();
504546

505547
// Act
506-
await client.PostAsync("https://localhost/api/test", new StringContent("{ \"id\":\"1\" }"));
548+
await client.PostAsync("https://localhost/api/test", new StringContent("{ \"id\":1 }"));
507549

508-
var act = () => mock.Requests.Should().ContainRequest()
509-
.WithBodyHavingProperty("id", "2");
550+
var act = () => mock.Requests.Should().ContainRequest().WithBodyHavingProperty("id", "2");
510551

511552
// Assert
512553
act.Should().Throw<XunitException>()

0 commit comments

Comments
 (0)