-
-
Notifications
You must be signed in to change notification settings - Fork 0
docs: document advanced paramter matching features #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Properties | ||
|
|
||
| Set up property getters and setters to control or verify property access on your mocks. | ||
|
|
||
| **Initialization** | ||
|
|
||
| You can initialize properties so they work like normal properties (setter changes the value, getter returns the last set | ||
| value): | ||
|
|
||
| ```csharp | ||
| sut.SetupMock.Property.TotalDispensed.InitializeWith(42); | ||
| ``` | ||
|
|
||
| **Returns / Throws** | ||
|
|
||
| Alternatively, set up properties with `Returns` and `Throws` (supports sequences): | ||
|
|
||
| ```csharp | ||
| sut.SetupMock.Property.TotalDispensed | ||
| .Returns(1) | ||
| .Returns(2) | ||
| .Throws(new Exception("Error")) | ||
| .Returns(4); | ||
| ``` | ||
|
|
||
| **Callbacks** | ||
|
|
||
| Register callbacks on the setter or getter: | ||
|
|
||
| ```csharp | ||
| sut.SetupMock.Property.TotalDispensed.OnGet.Do(() => Console.WriteLine("TotalDispensed was read!")); | ||
| sut.SetupMock.Property.TotalDispensed.OnSet.Do((oldValue, newValue) => Console.WriteLine($"Changed from {oldValue} to {newValue}!") ); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Methods | ||
|
|
||
| Use `mock.SetupMock.Method.MethodName(…)` to set up methods. You can specify argument matchers for each parameter. | ||
|
|
||
| ```csharp | ||
| // Setup Dispense to decrease stock and raise event | ||
| sut.SetupMock.Method.Dispense(It.Is("Dark"), It.IsAny<int>()) | ||
| .Returns((type, amount) => | ||
| { | ||
| var current = sut[type]; | ||
| if (current >= amount) | ||
| { | ||
| sut[type] = current - amount; | ||
| sut.RaiseOnMock.ChocolateDispensed(type, amount); | ||
| return true; | ||
| } | ||
| return false; | ||
| }); | ||
|
|
||
| // Setup method with callback | ||
| sut.SetupMock.Method.Dispense(It.Is("White"), It.IsAny<int>()) | ||
| .Do((type, amount) => Console.WriteLine($"Dispensed {amount} {type} chocolate.")); | ||
|
|
||
| // Setup method to throw | ||
| sut.SetupMock.Method.Dispense(It.Is("Green"), It.IsAny<int>()) | ||
| .Throws<InvalidChocolateException>(); | ||
| ``` | ||
|
|
||
| - Use `.Do(…)` to run code when the method is called. Supports parameterless or parameter callbacks. | ||
| - Use `.Returns(…)` to specify the value to return. You can provide a direct value, a callback, or a callback with | ||
| parameters. | ||
| - Use `.Throws(…)` to specify an exception to throw. Supports direct exceptions, exception factories, or factories with | ||
| parameters. | ||
| - Use `.Returns(…)` and `.Throws(…)` repeatedly to define a sequence of return values or exceptions (cycled on each | ||
| call). | ||
| - Use `.SkippingBaseClass(…)` to override the base class behavior for a specific method (only for class mocks). | ||
| - When you specify overlapping setups, the most recently defined setup takes precedence. | ||
|
|
||
| **Async Methods** | ||
|
|
||
| For `Task<T>` or `ValueTask<T>` methods, use `.ReturnsAsync(…)`: | ||
|
|
||
| ```csharp | ||
| sut.SetupMock.Method.DispenseAsync(It.IsAny<string>(), It.IsAny<int>()) | ||
| .ReturnsAsync(true); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Indexers | ||
|
|
||
| Set up indexers with argument matchers. Supports initialization, returns/throws sequences, and callbacks. | ||
|
|
||
| ```csharp | ||
| sut.SetupMock.Indexer(It.IsAny<string>()) | ||
| .InitializeWith(type => 20) | ||
| .OnGet.Do(type => Console.WriteLine($"Stock for {type} was read")); | ||
|
|
||
| sut.SetupMock.Indexer(It.Is("Dark")) | ||
| .InitializeWith(10) | ||
| .OnSet.Do((value, type) => Console.WriteLine($"Set [{type}] to {value}")); | ||
| ``` | ||
|
|
||
| - `.InitializeWith(…)` can take a value or a callback with parameters. | ||
| - `.Returns(…)` and `.Throws(…)` support direct values, callbacks, and callbacks with parameters and/or the current | ||
| value. | ||
| - `.OnGet.Do(…)` and `.OnSet.Do(…)` support callbacks with or without parameters. | ||
| - `.Returns(…)` and `.Throws(…)` can be chained to define a sequence of behaviors, which are cycled through on each | ||
| call. | ||
| - Use `.SkippingBaseClass(…)` to override the base class behavior for a specific indexer (only for class mocks). | ||
| - When you specify overlapping setups, the most recently defined setup takes precedence. | ||
|
|
||
| **Note**: | ||
| You can use the same [parameter matching](https://awexpect.com/docs/mockolate/setup/parameter-matching) | ||
| and [interaction](https://awexpect.com/docs/mockolate/setup/parameter-matching#parameter-interaction) options as for | ||
| methods. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.