Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ Verify that a method was called a specific number of times:
var sut = IMyService.CreateMock();
sut.MyMethod();

await That(sut.Mock.Verify.MyMethod()).Once(); // Exactly once
await That(sut.Mock.Verify.MyMethod()).Twice(); // Exactly twice
await That(sut.Mock.Verify.MyMethod()).Never(); // Never called
await That(sut.Mock.Verify.MyMethod()).AtLeastOnce(); // At least once
await That(sut.Mock.Verify.MyMethod()).AtLeastTwice(); // At least twice
await That(sut.Mock.Verify.MyMethod()).AtLeast(3.Times()); // At least 3 times
await That(sut.Mock.Verify.MyMethod()).AtMostOnce(); // At most once
await That(sut.Mock.Verify.MyMethod()).AtMostTwice(); // At most twice
await That(sut.Mock.Verify.MyMethod()).AtMost(4.Times()); // At most 4 times
await That(sut.Mock.Verify.MyMethod()).Exactly(2.Times()); // Exactly 2 times
await That(sut.Mock.Verify.MyMethod()).Once(); // Exactly once
await That(sut.Mock.Verify.MyMethod()).Twice(); // Exactly twice
await That(sut.Mock.Verify.MyMethod()).Never(); // Never called
await That(sut.Mock.Verify.MyMethod()).AtLeastOnce(); // At least once
await That(sut.Mock.Verify.MyMethod()).AtLeastTwice(); // At least twice
await That(sut.Mock.Verify.MyMethod()).AtLeast(3.Times()); // At least 3 times
await That(sut.Mock.Verify.MyMethod()).AtMostOnce(); // At most once
await That(sut.Mock.Verify.MyMethod()).AtMostTwice(); // At most twice
await That(sut.Mock.Verify.MyMethod()).AtMost(4.Times()); // At most 4 times
await That(sut.Mock.Verify.MyMethod()).Exactly(2.Times()); // Exactly 2 times
await That(sut.Mock.Verify.MyMethod()).Between(2).And(5.Times()); // Between 2 and 5 times
```

#### Asynchronous verification

With `Within(TimeSpan timeout)`, you can check whether the expected number of calls occurred within a given time
interval. This is useful for asynchronous or delayed invocations in the background.

`Within` and `WithCancellation` are available on `AtLeast*`, `Once`, `Twice`, `Exactly`, and `Between`. They are not
available on `Never` or `AtMost*`, since an upper bound cannot be confirmed by waiting longer.

```csharp
var sut = IMyService.CreateMock();

Expand Down Expand Up @@ -116,6 +120,8 @@ await That(sut.Mock.Verify).AllSetupsAreUsed();

### Web Extensions

> Web extensions require .NET 8.0 or later.

#### JSON Content

You can precisely verify JSON content in HTTP requests during your tests. This feature is
Expand All @@ -132,3 +138,15 @@ httpClient.Mock.Setup
.PostAsync(It.IsAny<Uri>(), It.IsHttpContent().WithJson("{\"bar\": \"baz\", \"foo\": 1}"))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));
```

By default, additional properties in the actual JSON are ignored. Use `IgnoringAdditionalProperties(false)` to require
an exact match:

```csharp
// Fails if the request body contains any property other than `foo` and `bar`
httpClient.Mock.Setup
.PostAsync(It.IsAny<Uri>(), It.IsHttpContent()
.WithJsonMatching(new { foo = 1, bar = "baz" })
.IgnoringAdditionalProperties(false))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));
```
Loading