-
-
Notifications
You must be signed in to change notification settings - Fork 0
docs: add documentation for delegates #428
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
4 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 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
File renamed without changes.
File renamed without changes.
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,7 @@ | ||
| { | ||
| "label": "Advanced Features", | ||
| "position": 5, | ||
| "link": { | ||
| "type": "generated-index" | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
Docs/pages/05-httpclient.md → Docs/pages/special-types/01-httpclient.md
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
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,92 @@ | ||
| # Delegates | ||
|
|
||
| Mockolate supports mocking delegates including `Action`, `Func<T>`, and custom delegates. | ||
|
|
||
| **Setup** | ||
|
|
||
| Use `SetupMock.Delegate(...)` to configure delegate behavior. | ||
|
|
||
| ```csharp | ||
| // Mock Action delegate | ||
| Action myAction = Mock.Create<Action>(); | ||
| myAction.SetupMock.Delegate().Do(() => Console.WriteLine("Action invoked!")); | ||
|
|
||
| // Mock Func<T> delegate | ||
| Func<int> myFunc = Mock.Create<Func<int>>(); | ||
| myFunc.SetupMock.Delegate().Returns(42); | ||
| ``` | ||
|
|
||
| For custom delegates with parameters: | ||
|
|
||
| ```csharp | ||
| // Define a custom delegate (typically declared at type level) | ||
| public delegate int Calculate(int x, string operation); | ||
|
|
||
| // Create and setup the mock | ||
| Calculate calculator = Mock.Create<Calculate>(); | ||
| calculator.SetupMock.Delegate(It.IsAny<int>(), It.Is("add")) | ||
| .Returns((x, operation) => x + 10); | ||
| ``` | ||
|
|
||
| Delegates with `ref` and `out` parameters are also supported: | ||
|
|
||
| ```csharp | ||
| // Define a custom delegate (typically declared at type level) | ||
| public delegate void ProcessData(int input, ref int value, out int result); | ||
|
|
||
| // Create and setup the mock | ||
| ProcessData processor = Mock.Create<ProcessData>(); | ||
| processor.SetupMock.Delegate(It.IsAny<int>(), It.IsRef<int>(v => v + 1), It.IsOut(() => 100)); | ||
| ``` | ||
|
|
||
| - Use `.Do(...)` to run code when the delegate is invoked. | ||
| - Use `.Returns(...)` to specify the return value for `Func<T>` delegates. | ||
| - Use `.Throws(...)` to specify an exception to throw. | ||
| - Use `.Returns(...)` and `.Throws(...)` repeatedly to define a sequence of behaviors. | ||
| - Full [parameter matching](https://awexpect.com/docs/mockolate/setup#parameter-matching) support for delegate | ||
| parameters including `ref` and `out` parameters. | ||
|
|
||
| **Verification** | ||
|
|
||
| You can verify that delegates were invoked with specific arguments: | ||
|
|
||
| ```csharp | ||
| // Verify Action was invoked at least once | ||
| Action myAction = Mock.Create<Action>(); | ||
| myAction.Invoke(); | ||
| myAction.VerifyMock.Invoked().AtLeastOnce(); | ||
|
|
||
| // Verify Func<T> was invoked exactly once | ||
| Func<int> myFunc = Mock.Create<Func<int>>(); | ||
| _ = myFunc(); | ||
| myFunc.VerifyMock.Invoked().Once(); | ||
| ``` | ||
|
|
||
| For custom delegates with parameters: | ||
|
|
||
| ```csharp | ||
| // Define a custom delegate (typically declared at type level) | ||
| public delegate int Calculate(int x, string operation); | ||
|
|
||
| // Create, invoke, and verify the mock | ||
| Calculate calculator = Mock.Create<Calculate>(); | ||
| _ = calculator(5, "add"); | ||
| calculator.VerifyMock.Invoked(It.IsAny<int>(), It.Is("add")).Once(); | ||
| ``` | ||
|
|
||
| Delegates with `ref` and `out` parameters are also supported: | ||
|
|
||
| ```csharp | ||
| // Define a custom delegate (typically declared at type level) | ||
| public delegate void ProcessData(int input, ref int value, out int result); | ||
|
|
||
| // Create, invoke, and verify the mock | ||
| ProcessData processor = Mock.Create<ProcessData>(); | ||
| int val = 0; | ||
| processor(1, ref val, out int res); | ||
| processor.VerifyMock.Invoked(It.IsAny<int>(), It.IsRef<int>(), It.IsOut<int>()).Once(); | ||
| ``` | ||
|
|
||
| **Note:** | ||
| Delegate parameters also | ||
| support [argument matchers](https://awexpect.com/docs/mockolate/verify-interactions#argument-matchers). | ||
|
vbreuss marked this conversation as resolved.
|
||
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,7 @@ | ||
| { | ||
| "label": "Special Types", | ||
| "position": 6, | ||
| "link": { | ||
| "type": "generated-index" | ||
| } | ||
| } |
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
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
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.