Skip to content

Commit 39ec951

Browse files
author
Arthur Aulicino
committed
feat: improvements on usage instructions
1 parent 08391b1 commit 39ec951

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

README.md

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,30 @@ If you don't have NSubstitute already from another source, add this to your **ma
1919
```
2020

2121
To install this package, place this in your **manifest.json**:
22+
2223
```json
2324
"com.aaulicino.unity-coroutines-for-nsubstitute": "https://github.com/AAulicino/Unity-Coroutines-for-NSubstitute.git"
2425
```
2526

2627
## Basic use
2728

28-
Let's use this simple counter class using coroutines for testing:
29+
### Creating the mock
30+
31+
You can mock the ICoroutineRunner interface by calling:
32+
33+
```csharp
34+
ICoroutineRunner runner = CoroutineSubstitute.Create();
35+
```
36+
37+
To preserve the Syntax provided by NSubstitute, this alternate version can be used instead:
38+
39+
```csharp
40+
ICoroutineRunner runner = Substitute.ForPartsOf<CoroutineRunnerSubstitute>();
41+
```
42+
43+
### Using the mock
44+
45+
Let's use this simple counter class for testing:
2946

3047
```csharp
3148
public class Counter
@@ -62,26 +79,8 @@ Let's use this simple counter class using coroutines for testing:
6279
}
6380
```
6481

65-
One thing you might've noticed is that instead of directly referencing a MonoBehaviour to call
66-
StartCoroutine, we're using an interface for the runner. This allows us to mock the runner in our
67-
tests.
68-
69-
### Creating the mock
70-
71-
To mock the ICoroutineRunner we need to create a mock for it. A simplified way to do so is by
72-
calling:
73-
74-
```
75-
ICoroutineRunner runner = CoroutineSubstitute.Create();
76-
```
77-
78-
To preserve the Syntax provided by NSubstitute, this alternate version can be used instead:
79-
80-
```
81-
ICoroutineRunner runner = Substitute.ForPartsOf<CoroutineRunnerSubstitute>();
82-
```
83-
84-
### Using the mock
82+
One thing you might've noticed is that instead of calling StartCoroutine on a MonoBehaviour,
83+
we're calling it on the ICoroutineRunner interface. This allows us to mock the runner in our tests.
8584

8685
The Counter can now be tested as follows:
8786

@@ -96,7 +95,7 @@ The Counter can now be tested as follows:
9695
Assert.AreEqual(1, Counter.Current);
9796
```
9897

99-
Calling Runner.MoveNext() will simulate Unity's coroutine update loop.
98+
Calling `Runner.MoveNext()` will simulate Unity's coroutine update loop.
10099

101100
You can check the [CounterTests.cs](https://github.com/AAulicino/Unity-Coroutines-for-NSubstitute/blob/main/Tests/Editor/Samples/Counter/CounterTests.cs)
102101
for test examples on the [Counter](https://github.com/AAulicino/Unity-Coroutines-for-NSubstitute/blob/main/Tests/Editor/Samples/Counter/Counter.cs) class.
@@ -105,20 +104,13 @@ Since MonoBehaviours implement all methods specified in the ICoroutineRunner int
105104
simply add it to your MonoBehaviour, for example:
106105

107106
```csharp
108-
public class MyCoroutineRunner : MonoBehaviour, ICoroutineRunner
109-
{
110-
}
111-
```
112-
113-
```csharp
114-
public class GameSetup : MonoBehaviour
107+
public class GameSetup : MonoBehaviour, ICoroutineRunner
115108
{
116-
[SerializeField] public MyCoroutineRunner coroutineRunner;
117109
Counter counter;
118110

119111
void Start ()
120112
{
121-
counter = new Counter(coroutineRunner);
113+
counter = new Counter(this);
122114
counter.Start();
123115
}
124116
}
Binary file not shown.

0 commit comments

Comments
 (0)