You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Only use Moq APIs inside `MoqProvider` or Moq‑specific test code.
31
33
2.**Reuse shared helpers**
32
34
- For component creation, always call `MockerConstructionHelper.CreateInstance`.
35
+
- When a test must choose a constructor, prefer `MockerTestBase<TComponent>.ComponentConstructorParameterTypes` first. For direct `Mocker` usage, prefer `CreateInstanceByType(...)`. Use `CreateComponentAction` only when constructor selection cannot be expressed as a signature.
36
+
- When framework code needs a real `IServiceProvider` or `IServiceScopeFactory`, prefer `CreateTypedServiceProvider(...)` or `AddServiceProvider(...)` over mocked `IServiceProvider` shims or registering only `IServiceScopeFactory` from a manually built provider.
33
37
- For verification, follow the `VerifyLogger` pattern.
Copy file name to clipboardExpand all lines: docs/getting-started/README.md
+31Lines changed: 31 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,6 +92,7 @@ Use this table when you are deciding which package line your test project should
92
92
Important package boundaries in the current v4 line:
93
93
94
94
`FastMoq` already includes the common end-user surface, including shared Azure SDK helpers, web, database, and Azure Functions helpers
95
+
95
96
-`FastMoq` also includes the FastMoq analyzer assets by default so most test projects get migration guidance without extra setup
96
97
-`FastMoq.Core` stays lighter on purpose, so shared Azure SDK helpers, EF helpers, Azure Functions helpers, and web helpers are separate package decisions when you consume core directly
97
98
-`FastMoq.Core` does not include analyzer assets; add `FastMoq.Analyzers` explicitly if you want analyzer guidance in a core-only package graph
@@ -379,6 +380,36 @@ public class OrderServiceTests : MockerTestBase<OrderService>
379
380
}
380
381
```
381
382
383
+
When a test needs a specific constructor, prefer the explicit constructor-selection hooks instead of relying on `GetObject<T>()` to land on the right overload.
384
+
385
+
- For `MockerTestBase<TComponent>`, override `ComponentConstructorParameterTypes`.
386
+
- For direct `Mocker` usage, call `CreateInstanceByType<T>(...)`.
387
+
- If the chosen constructor depends on `IServiceProvider` or `IServiceScopeFactory`, build and register a typed provider with `AddServiceProvider(...)` instead of extracting only `IServiceScopeFactory` from a manual `BuildServiceProvider()` call.
See the [Testing Guide](./testing-guide.md#explicit-constructor-selection-in-tests) for the full constructor-selection rules and the [typed `IServiceProvider` helper guidance](./testing-guide.md#typed-iserviceprovider-helpers) for framework-heavy ServiceCollection patterns.
Copy file name to clipboardExpand all lines: docs/getting-started/testing-guide.md
+32-6Lines changed: 32 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,12 +7,14 @@ This guide documents the testing patterns that match FastMoq's current behavior
7
7
Use these rules first:
8
8
9
9
1. Use [MockerTestBase<TComponent>](xref:FastMoq.MockerTestBase`1) when you want FastMoq to create the component under test and manage its dependencies.
10
-
2. Use [Mocks.GetOrCreateMock<T>()](xref:FastMoq.Mocker.GetOrCreateMock``1(FastMoq.MockRequestOptions)) when you want the normal FastMoq tracked mock path for a dependency.
11
-
3. Use [AddType(...)](xref:FastMoq.Mocker.AddType``1(System.Func{FastMoq.Mocker,``0},System.Boolean,System.Object[])) when you need to replace FastMoq's default resolution with a specific concrete type, factory, or fixed instance.
12
-
4. Use `CreateTypedServiceProvider(...)` and `AddServiceProvider(...)` when framework code expects a typed `IServiceProvider` rather than a one-object-for-all-types shim.
13
-
5. If the constructor uses the same abstraction more than once under different DI service keys, use keyed mocks, keyed registrations, or explicit constructor injection for tests where dependency selection matters.
14
-
6. Use [AddKnownType(...)](xref:FastMoq.Mocker.AddKnownType(FastMoq.KnownTypeRegistration,System.Boolean)) when a framework-style type needs special resolution or post-processing behavior.
15
-
7. Use [GetMockDbContext<TContext>()](xref:FastMoq.DbContextMockerExtensions.GetMockDbContext``1(FastMoq.Mocker)) when testing EF Core contexts. Do not hand-roll DbContext setup unless you need behavior outside FastMoq's helper.
10
+
2. If a `MockerTestBase<TComponent>` test must force a specific constructor, override `ComponentConstructorParameterTypes` first. Use `CreateComponentAction` only when the test needs custom creation logic beyond selecting a signature.
11
+
3. Use [Mocks.GetOrCreateMock<T>()](xref:FastMoq.Mocker.GetOrCreateMock``1(FastMoq.MockRequestOptions)) when you want the normal FastMoq tracked mock path for a dependency.
12
+
4. Use [AddType(...)](xref:FastMoq.Mocker.AddType``1(System.Func{FastMoq.Mocker,``0},System.Boolean,System.Object[])) when you need to replace FastMoq's default resolution with a specific concrete type, factory, or fixed instance.
13
+
5. Use `CreateInstanceByType(...)` when direct `Mocker` usage must pick an exact constructor signature. Do not treat `GetObject<T>()` as the explicit constructor-selection API.
14
+
6. Use `CreateTypedServiceProvider(...)` and `AddServiceProvider(...)` when framework code expects a typed `IServiceProvider` rather than a one-object-for-all-types shim.
15
+
7. If the constructor uses the same abstraction more than once under different DI service keys, use keyed mocks, keyed registrations, or explicit constructor injection for tests where dependency selection matters.
16
+
8. Use [AddKnownType(...)](xref:FastMoq.Mocker.AddKnownType(FastMoq.KnownTypeRegistration,System.Boolean)) when a framework-style type needs special resolution or post-processing behavior.
17
+
9. Use [GetMockDbContext<TContext>()](xref:FastMoq.DbContextMockerExtensions.GetMockDbContext``1(FastMoq.Mocker)) when testing EF Core contexts. Do not hand-roll DbContext setup unless you need behavior outside FastMoq's helper.
`AddServiceProvider(...)` registers the typed provider itself and, when the built container exposes them, also registers `IServiceScopeFactory` and `IServiceProviderIsService` for the current `Mocker`.
89
+
90
+
If a constructor takes `IServiceScopeFactory`, prefer this shape:
instead of building a provider manually and registering only `provider.GetRequiredService<IServiceScopeFactory>()`. Keeping the full typed provider registered makes constructor injection, nested framework resolution, and service-scope behavior stay aligned.
108
+
109
+
For Azure-oriented tests that also need configuration defaults, prefer `CreateAzureServiceProvider(...)` or `AddAzureServiceProvider(...)` from `FastMoq.Azure.DependencyInjection` instead of repeating `AddLogging()`, `AddOptions()`, and `IConfiguration` setup in every test.
110
+
86
111
Use `CreateFunctionContextInstanceServices(...)` and `AddFunctionContextInstanceServices(...)` for Azure Functions worker tests instead of hand-writing `FunctionContext.InstanceServices` plumbing:
87
112
88
113
```csharp
@@ -206,6 +231,7 @@ FastMoq handles constructor resolution and injection at runtime; it does not byp
206
231
207
232
When a test needs a specific constructor, prefer a test-side override first.
208
233
That keeps constructor choice inside the test harness and avoids changing production code only to satisfy test setup.
234
+
If the selected constructor depends on `IServiceProvider` or `IServiceScopeFactory`, pair the constructor-selection hook with `AddServiceProvider(...)` from the earlier typed-provider section instead of registering only a manually extracted scope factory.
209
235
210
236
For `MockerTestBase<TComponent>`, override `ComponentConstructorParameterTypes` when you want a specific signature but still want the default FastMoq creation path:
0 commit comments