|
1 | 1 | # Methods |
2 | 2 |
|
3 | | -Start by creating a mock of the interface or class you want to mock. |
| 3 | +__TL;DR__ |
4 | 4 |
|
5 | 5 | ```csharp |
6 | | -var mockVersionLibrary = Mock.IVersionLibrary(config => config. |
7 | | - [Your mock setup here] |
| 6 | +public interface IVersionLibrary |
| 7 | +{ |
| 8 | + bool DownloadExists(string version); |
| 9 | + bool DownloadExists(Version version); |
| 10 | + Task<Uri> DownloadLinkAsync(string version); |
| 11 | + } |
| 12 | + |
| 13 | +var versionLibrary = Mock.IVersionLibrary(config => config |
| 14 | + .DownloadExists(returns: true) // Returns true for all versions |
| 15 | + .DownloadExists(throws: new IndexOutOfRangeException()) // Throws IndexOutOfRangeException for all versions |
| 16 | + .DownloadExists(call: s => s.StartsWith(value: "2.0.0")) // Returns true for version 2.0.0.x base on a string parameter |
| 17 | + .DownloadExists(call: v => v is { Major: 2, Minor: 0, Revision: 0 }) // Returns true for version 2.0.0.x based on a version parameter |
| 18 | + .DownloadExists(returnValues: [true, true, false]) // Returns true two times, then false |
| 19 | +
|
| 20 | + .DownloadLinkAsync(returns: Task.FromResult(result: new Uri(uriString: "http://downloads/2.0.0"))) // Returns a task containing a download link for all versions |
| 21 | + .DownloadLinkAsync(call: s => Task.FromResult(result: s.StartsWith(value: "2.0.0") ? new Uri(uriString: "http://downloads/2.0.0") : new Uri(uriString: "http://downloads/UnknownVersion"))) // Returns a task containing a download link for version 2.0.0.x otherwise a error link |
| 22 | + .DownloadLinkAsync(throws: new TaskCanceledException()) // Throws IndexOutOfRangeException for all parameters |
| 23 | + .DownloadLinkAsync(returns: new Uri(uriString: "http://downloads/2.0.0")) // Returns a task containing a download link for all versions |
| 24 | + .DownloadLinkAsync(call: s => s.StartsWith(value: "2.0.0") ? new Uri(uriString: "http://downloads/2.0.0") : new Uri(uriString: "http://downloads/UnknownVersion")) // Returns a task containing a download link for version 2.0.0.x otherwise a error link |
| 25 | + .DownloadLinkAsync(returnValues: [Task.FromResult(result: new Uri(uriString: "http://downloads/1.0.0")), Task.FromResult(result: new Uri(uriString: "http://downloads/1.1.0")), Task.FromResult(result: new Uri(uriString: "http://downloads/2.0.0"))]) // Returns a task with a download link |
| 26 | + .DownloadLinkAsync(returnValues: [new Uri(uriString: "http://downloads/2.0.0"), new Uri(uriString: "http://downloads/2.0.0"), new Uri(uriString: "http://downloads/2.0.0")]) // Returns a task with a download link |
8 | 27 | ); |
| 28 | + |
| 29 | +// Inject into system under test |
9 | 30 | ``` |
10 | | -Please note |
| 31 | + |
| 32 | +__Please note__ |
11 | 33 |
|
12 | 34 | - Multiple specifications for a method will overwrite each other with the last one taking precedence. |
13 | 35 | - Parameter-names can be omitted but makes the code more readable. |
|
0 commit comments