Skip to content

Commit 435c0a3

Browse files
committed
Update FAQs
1 parent 075b3a4 commit 435c0a3

1 file changed

Lines changed: 57 additions & 55 deletions

File tree

FAQs.md

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -154,63 +154,29 @@ The component under test is created before the test-class constructor body finis
154154
### Option 1: Base Class Constructor
155155

156156
This example shows setup in the base constructor callback. The code cannot depend on instance state from the test class.
157+
These examples assume the Moq provider extensions are available for the arrange-time `Setup(...)` calls.
157158

158159
```c#
159-
public class TestMyService : MockerTestBase<MyService> {
160-
public TestMyService() : base(setupMocksAction: m => {
161-
m.OptionalParameterResolution = OptionalParameterResolutionMode.ResolveViaMocker;
162-
163-
m.GetMock<IProfileRepo>().Setup(x => x.GetProfileAsync(It.IsAny<string>()))
164-
.ReturnsAsync(() => [new ProfileEntity { ProfileType = "test" }]);
165-
166-
m.Initialize<IConfigRepo>(configMock =>
167-
{
168-
configMock.Setup(x => x.GetTypeByIdAsync(id)).ReturnsAsync(() =>
169-
new TypeEntity
170-
{
171-
// Type code here
172-
}
173-
);
174-
configMock.Setup(x => x.GetTypesAsync()).ReturnsAsync(
175-
() =>
176-
[
177-
new TypeEntity
178-
{
179-
// Type code here
180-
},
181-
new TypeEntity
182-
{
183-
// Type code here
184-
},
185-
]
186-
);
187-
});
188-
})
189-
}
190-
```
160+
public class TestMyService : MockerTestBase<MyService>
161+
{
162+
public TestMyService()
163+
: base(setupMocksAction: mocker =>
164+
{
165+
const int typeId = 42;
191166

192-
### Option 2: Override SetupMocksAction
167+
mocker.OptionalParameterResolution = OptionalParameterResolutionMode.ResolveViaMocker;
193168

194-
This example shows setup in the `SetupMocksAction` property. The code can use instance state from the test class.
169+
mocker.GetOrCreateMock<IProfileRepo>()
170+
.Setup(x => x.GetProfileAsync(It.IsAny<string>()))
171+
.ReturnsAsync([new ProfileEntity { ProfileType = "test" }]);
195172

196-
```c#
197-
protected override Action<Mocker> SetupMocksAction => m =>
198-
{
199-
m.OptionalParameterResolution = OptionalParameterResolutionMode.ResolveViaMocker;
200-
201-
m.GetMock<IProfileRepo>().Setup(x => x.GetProfileAsync(It.IsAny<string>()))
202-
.ReturnsAsync(() => [new ProfileEntity { ProfileType = "test" }]);
203-
204-
m.Initialize<IConfigRepo>(configMock =>
205-
{
206-
configMock.Setup(x => x.GetTypeByIdAsync(id)).ReturnsAsync(() =>
207-
new TypeEntity
208-
{
209-
// Type code here
210-
}
211-
);
212-
configMock.Setup(x => x.GetTypesAsync()).ReturnsAsync(
213-
() =>
173+
var configRepo = mocker.GetOrCreateMock<IConfigRepo>();
174+
configRepo.Setup(x => x.GetTypeByIdAsync(typeId)).ReturnsAsync(
175+
new TypeEntity
176+
{
177+
// Type code here
178+
});
179+
configRepo.Setup(x => x.GetTypesAsync()).ReturnsAsync(
214180
[
215181
new TypeEntity
216182
{
@@ -220,9 +186,45 @@ protected override Action<Mocker> SetupMocksAction => m =>
220186
{
221187
// Type code here
222188
},
223-
]
224-
);
225-
});
189+
]);
190+
})
191+
{
192+
}
193+
}
194+
```
195+
196+
### Option 2: Override SetupMocksAction
197+
198+
This example shows setup in the `SetupMocksAction` property. The code can use instance state from the test class.
199+
200+
```c#
201+
protected override Action<Mocker> SetupMocksAction => mocker =>
202+
{
203+
const int typeId = 42;
204+
205+
mocker.OptionalParameterResolution = OptionalParameterResolutionMode.ResolveViaMocker;
206+
207+
mocker.GetOrCreateMock<IProfileRepo>()
208+
.Setup(x => x.GetProfileAsync(It.IsAny<string>()))
209+
.ReturnsAsync([new ProfileEntity { ProfileType = "test" }]);
210+
211+
var configRepo = mocker.GetOrCreateMock<IConfigRepo>();
212+
configRepo.Setup(x => x.GetTypeByIdAsync(typeId)).ReturnsAsync(
213+
new TypeEntity
214+
{
215+
// Type code here
216+
});
217+
configRepo.Setup(x => x.GetTypesAsync()).ReturnsAsync(
218+
[
219+
new TypeEntity
220+
{
221+
// Type code here
222+
},
223+
new TypeEntity
224+
{
225+
// Type code here
226+
},
227+
]);
226228
};
227229
```
228230

0 commit comments

Comments
 (0)