Skip to content

Commit 800653d

Browse files
committed
Fixed broken link
Added documentation about construction, indexers and events
1 parent bca7b8c commit 800653d

File tree

8 files changed

+163
-9
lines changed

8 files changed

+163
-9
lines changed

docs/ADR/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ All the ADRs have been approved and are considered final decisions for the proje
2424
- [Support for Events](feature/SupportForEvents.md) - Decision on supporting events in the mocking framework.
2525
- [Support for Indexers](feature/SupportForIndexers.md) - Decision on supporting indexers in the mocking framework.
2626
- Special cases
27-
- [Allowing Skipping Arguments in Mock Setup](general/SupportSkippingArguments.md) - Allows skipping arguments in mock setups for flexibility.
27+
- [Allowing Skipping Arguments in Mock Setup](feature/SupportSkippingArguments.md) - Allows skipping arguments in mock setups for flexibility.
2828
- [Support for Protected Methods](feature/SupportingProtectedMethods.md) - Decision on whether to support mocking protected methods.
2929
- [Support for Generic Methods (WIP)](feature/SupportForGenericMethods.md) - Decision on supporting generic methods in the mocking framework.
3030
- [Support for Asynchronous Methods (WIP)](feature/SupportForAsynchronousMethods.md) - Handling asynchronous methods in the mocking framework.

docs/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ MiniMock offers a _minimalistic_ approach to mocking in .NET with a focus on sim
3232

3333
Try it out or continue with [Getting started](guide/getting-started.md) to learn more or read the [Mocking guidelines](guide/mocking-guidelines.md) to get a better understanding of when, why and how to mock and when not to.
3434

35-
For more details on specific aspects you can read about [Methods](guide/methods.md), [Properties](guide/properties.md), [Events](guide/events.md) or
36-
[Indexers](guide/indexers.md).
35+
For more details on specific aspects you can read about [Construction](guide/construction.md) [Methods](guide/methods.md), [Properties](guide/properties.md), [Events](guide/events.md) or
36+
[Indexers](guide/indexers.md).
3737
Or you shift into high gear with [Advanced Features](advanced-features.md), [FaQ](faq.md) or [Troubleshooting](troubleshooting.md).
3838

39-
If you are more into the ins and outs of MiniMock you can read the [Motivation](motivation.md) behind MiniMock or the [ADRs](../ADR/README.md).
39+
If you are more into the ins and outs of MiniMock you can read the [Motivation](motivation.md) behind MiniMock or the [ADRs](ADR/README.md).

docs/guide/construction.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Construction
2+
3+
__TL;DR__
4+
5+
```csharp
6+
[Fact]
7+
[Mock<IVersionLibrary>] // Signals minimock to build a mock object for IVersionLibrary.
8+
public void MockInitialization()
9+
{
10+
// Mock without anything mocked used to satisfy dependencies not used in the tests execution path
11+
var emptyMock = Mock.IVersionLibrary();
12+
13+
// Mock with inline configuration useful for most setup scenarios
14+
var inlineMock = Mock.IVersionLibrary(config => config
15+
.DownloadExists(true)
16+
);
17+
18+
// Mock with external configuration useful for more complex scenarios like testing events and modifying mock behaviour.
19+
var externalMock = Mock.IVersionLibrary(out var config);
20+
config.DownloadExists(true);
21+
22+
// Direct access to the mock implementation.
23+
var implementationMock = new MockOf_IVersionLibrary(config => config
24+
.DownloadExists(true)
25+
);
26+
}
27+
```
28+
29+
~~~~

docs/guide/events.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Events
2+
3+
__TL;DR__
4+
5+
```csharp
6+
public interface IVersionLibrary
7+
{
8+
event EventHandler<Version> NewVersionAdded;
9+
}
10+
11+
Action<Version> triggerNewVersionAdded = _ => { };
12+
13+
var versionLibrary = Mock.IVersionLibrary(config => config
14+
.NewVersionAdded(raise: new Version(2, 0, 0, 0)) // Raises the event right away
15+
.NewVersionAdded(trigger: out triggerNewVersionAdded) // Provides a trigger for when a new version is added
16+
);
17+
18+
// Inject into system under test
19+
20+
triggerNewVersionAdded(new Version(2, 0, 0, 0));
21+
```
22+
23+
Alternative to creating a action for triggering the is to use the out parameter for configuration instead.
24+
```csharp
25+
var versionLibrary = Mock.IVersionLibrary(out var config);
26+
27+
// Inject into system under test
28+
29+
config.NewVersionAdded(raise: new Version(2, 0, 0, 0));
30+
```
31+
32+
__Please note__
33+
34+
- Parameter-names can be omitted but makes the code more readable.
35+
- Unlike other members, events does not need to be specified in order to be subscribed to by the system under test.

docs/guide/indexers.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Indexers
2+
3+
__TL;DR__
4+
5+
```csharp
6+
public interface IVersionLibrary
7+
{
8+
Version this[string key] { get; set; }
9+
}
10+
11+
var versions = new Dictionary<string, Version> { { "current", new Version(2, 0, 0, 0) } };
12+
13+
var versionLibrary = Mock.IVersionLibrary(config => config
14+
.Indexer(get: key => new Version(2, 0, 0, 0), set: (key, value) => { }) // Overwrites the indexer getter and setter
15+
.Indexer(values: versions) // Provides a dictionary to retrieve and store versions
16+
);
17+
18+
// Inject into system under test
19+
```
20+
21+
__Please note__
22+
23+
- Multiple specifications for an indexer will overwrite each other with the last one taking precedence.
24+
- Parameter-names can be omitted but make the code more readable.
25+
- Any indexer that is not explicitly specified will throw an `InvalidOperationException` when called.

docs/guide/methods.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
# Methods
22

3-
Start by creating a mock of the interface or class you want to mock.
3+
__TL;DR__
44

55
```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
827
);
28+
29+
// Inject into system under test
930
```
10-
Please note
31+
32+
__Please note__
1133

1234
- Multiple specifications for a method will overwrite each other with the last one taking precedence.
1335
- Parameter-names can be omitted but makes the code more readable.

docs/guide/properties.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
# Properties
22

3-
__WIP__
3+
__TL;DR__
4+
5+
```csharp
6+
public interface IVersionLibrary
7+
{
8+
Version CurrentVersion { get; set; }
9+
}
10+
11+
var versionLibrary = Mock.IVersionLibrary(config => config
12+
.CurrentVersion(get: () => new Version(major: 2, minor: 0, build: 0, revision: 0), set: version => throw new IndexOutOfRangeException()) // Overwrites the property getter and setter
13+
.CurrentVersion(value: new Version(major: 2, minor: 0, build: 0, revision: 0)) // Sets the initial version to 2.0.0.0
14+
);
15+
16+
// Inject into system under test
17+
18+
```
19+
20+
__Please note__
21+
22+
- Multiple specifications for a property will overwrite each other with the last one taking precedence.
23+
- Parameter-names can be omitted but make the code more readable.
24+
- Any property that is not explicitly specified will throw an `InvalidOperationException` when called.

tests/MiniMock.Tests/Demo.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ public async Task TestingAllTheOptions()
7070
triggerNewVersionAdded(new Version(2, 0, 0, 0));
7171
}
7272

73+
[Fact]
74+
[Mock<IVersionLibrary>]
75+
public void MockInitialization()
76+
{
77+
// Mock without anything mocked used to satisfy dependencies not used in the tests execution path
78+
var emptyMock = Mock.IVersionLibrary();
79+
80+
// Mock with inline configuration useful for most setup scenarios
81+
var inlineMock = Mock.IVersionLibrary(config => config
82+
.DownloadExists(true)
83+
);
84+
85+
// Mock with external configuration useful for more complex scenarios like testing events and modifying mock behaviour.
86+
var externalMock = Mock.IVersionLibrary(out var config);
87+
config.DownloadExists(true);
88+
89+
// Direct access to the mock implementation.
90+
var implementationMock = new MockOf_IVersionLibrary(config => config
91+
.DownloadExists(true)
92+
);
93+
}
94+
7395
[Fact]
7496
[Mock<IVersionLibrary>]
7597
public void SimpleReturnValue()

0 commit comments

Comments
 (0)