Adds Verify support for verifying Mockly types.
See Milestones for release notes.
Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.
[ModuleInitializer]
public static void Init() =>
VerifyMockly.Initialize();An HttpMock and its captured requests can then be verified:
[Test]
public async Task VerifyGetRequest()
{
var mock = new HttpMock();
mock.ForGet()
.ForHttps()
.ForHost("api.example.com")
.WithPath("/api/users/123")
.RespondsWithJsonContent(new { id = 123, name = "John" });
var client = mock.GetClient();
await client.GetAsync("https://api.example.com/api/users/123");
await Verify(mock);
}Results in:
[
{
Method: GET,
Scheme: https,
Host: api.example.com,
Path: /api/users/123,
WasExpected: true,
StatusCode: OK
}
][Test]
public async Task VerifyPostRequest()
{
var mock = new HttpMock();
mock.ForPost()
.ForHttps()
.ForHost("api.example.com")
.WithPath("/api/users")
.RespondsWithStatus(System.Net.HttpStatusCode.Created);
var client = mock.GetClient();
await client.PostAsync(
"https://api.example.com/api/users",
new StringContent(
"""{"name":"Jane"}""",
Encoding.UTF8,
"application/json"));
await Verify(mock);
}Results in:
[
{
Method: POST,
Scheme: https,
Host: api.example.com,
Path: /api/users,
Body: {"name":"Jane"},
WasExpected: true,
StatusCode: Created
}
][Test]
public async Task VerifyRequestCollection()
{
var mock = new HttpMock();
var requests = new RequestCollection();
mock.ForGet()
.ForHttps()
.ForHost("api.example.com")
.WithPath("/api/users/*")
.CollectingRequestsIn(requests)
.RespondsWithJsonContent(new { id = 1, name = "John" });
var client = mock.GetClient();
await client.GetAsync("https://api.example.com/api/users/1");
await client.GetAsync("https://api.example.com/api/users/2");
await Verify(requests);
}Results in:
[
{
Method: GET,
Scheme: https,
Host: api.example.com,
Path: /api/users/1,
WasExpected: true,
StatusCode: OK
},
{
Method: GET,
Scheme: https,
Host: api.example.com,
Path: /api/users/2,
WasExpected: true,
StatusCode: OK
}
]Members can be scrubbed by name:
[Test]
public async Task ScrubBody()
{
var mock = new HttpMock();
mock.ForPost()
.ForHttps()
.ForHost("api.example.com")
.WithPath("/api/users")
.RespondsWithStatus(System.Net.HttpStatusCode.Created);
var client = mock.GetClient();
await client.PostAsync(
"https://api.example.com/api/users",
new StringContent(
"""{"name":"Jane"}""",
Encoding.UTF8,
"application/json"));
await Verify(mock)
.ScrubMember("Body");
}Results in:
[
{
Method: POST,
Scheme: https,
Host: api.example.com,
Path: /api/users,
Body: Scrubbed,
WasExpected: true,
StatusCode: Created
}
]