Skip to content

Commit 8c31fa9

Browse files
authored
Docs: Clarifies source generator ambiguities and disabling (#410) (#411)
Adds documentation explaining ambiguous reference issues caused by internal partial source-generated classes, details how to disable specific generators via MSBuild properties, and updates README navigation to source generator docs.
1 parent b140ecf commit 8c31fa9

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ car.Accelerate(42);
6464
// This method verifies all mocks in the container
6565
mocker.VerifyAll();
6666
```
67+
68+
Documentation
69+
=============
70+
71+
For more detailed documentation, including information about the built-in source generators that can automatically generate test boilerplate code, see the [docs folder](docs/).
72+
73+
- [AutoMocker API Reference](docs/Moq.AutoMock.md)
74+
- [Source Generators](docs/SourceGenerators.md) - Learn about automatic code generation for constructor tests, options configuration, logging, and more

docs/SourceGenerators.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,64 @@ var service = mocker.CreateInstance<MyService>();
108108

109109
[Learn more →](SourceGenerators/KeyedServicesExtensionGenerator.md)
110110

111+
## Important: Generated Classes Are Internal Partials
112+
113+
All extension classes created by these source generators are generated as **partial classes** with **internal visibility**. For example, the Keyed Services generator produces:
114+
115+
```csharp
116+
internal static partial class AutoMockerKeyedServicesExtensions
117+
{
118+
// Generated extension methods...
119+
}
120+
```
121+
122+
### Ambiguous Reference Issues with Multiple Test Projects
123+
124+
This design can cause **ambiguous method call errors** when multiple test projects reference each other and both have the source generators enabled. Since each project generates its own internal partial class with the same name and methods, projects that share visibility (e.g., via `InternalsVisibleTo` or project references) may see duplicate definitions.
125+
126+
**Example error:**
127+
```
128+
The call is ambiguous between the following methods or properties:
129+
'Moq.AutoMock.AutoMockerKeyedServicesExtensions.WithKeyedService(...)' and
130+
'Moq.AutoMock.AutoMockerKeyedServicesExtensions.WithKeyedService(...)'
131+
```
132+
133+
This commonly occurs when:
134+
- An integration test project references a unit test project
135+
- `InternalsVisibleTo` is used between test projects
136+
- Both projects reference `Moq.AutoMock` and the same packages that trigger generators (e.g., `Microsoft.Extensions.DependencyInjection.Abstractions`)
137+
138+
**Solution:** Disable the source generator in one of the projects (see [Disabling Source Generators](#disabling-source-generators) below).
139+
140+
For more details, see [Issue #410](https://github.com/moq/Moq.AutoMocker/issues/410).
141+
142+
## Disabling Source Generators
143+
144+
Each source generator can be individually disabled using MSBuild properties in your project's `.csproj` file:
145+
146+
| Generator | MSBuild Property |
147+
|-----------|-----------------|
148+
| Options Extension | `EnableMoqAutoMockerOptionsGenerator` |
149+
| Keyed Services Extension | `EnableMoqAutoMockerKeyedServicesGenerator` |
150+
| Fake Logging Extension | `EnableMoqAutoMockerFakeLoggingGenerator` |
151+
| Application Insights Extension | `EnableMoqAutoMockerApplicationInsightsGenerator` |
152+
153+
**Example: Disabling a generator**
154+
```xml
155+
<PropertyGroup>
156+
<!-- Disable the Keyed Services generator -->
157+
<EnableMoqAutoMockerKeyedServicesGenerator>false</EnableMoqAutoMockerKeyedServicesGenerator>
158+
</PropertyGroup>
159+
```
160+
161+
**Example: Disabling multiple generators**
162+
```xml
163+
<PropertyGroup>
164+
<EnableMoqAutoMockerKeyedServicesGenerator>false</EnableMoqAutoMockerKeyedServicesGenerator>
165+
<EnableMoqAutoMockerOptionsGenerator>false</EnableMoqAutoMockerOptionsGenerator>
166+
</PropertyGroup>
167+
```
168+
111169
## Tips and Best Practices
112170

113171
### Review Generated Code

0 commit comments

Comments
 (0)