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
{{ message }}
Repository navigation
TUnit.Mocks: mocking one type through both Mock() and Mock.Wrap aborts the generator (duplicate hintName) #6834
Reaching the same type through both a regular mock (T.Mock() / Mock.Of<T>()) and Mock.Wrap(new T()) in the same compilation aborts the source generator:
warning CS8785: Generator 'MockGenerator' failed to generate source. It will not contribute to the
output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with
message 'The hintName 'MyNamespace_MyType_MockImplFactory.g.cs' of the added source file must be
unique within a generator. (Parameter 'hintName')'
Because the generator aborts, every mock in the compilation disappears and the build fails with a
cascade of CS1061 "does not contain a definition for ..." errors on unrelated mocks. The real cause
is only visible in the CS8785 warning.
Either test on its own compiles. Together they do not.
Cause
Mock.Of<T>() and Mock.Wrap(instance) produce two MockTypeModels for the same type that differ
only in IsWrapMock. Model equality includes that flag, so both survive dedup in MockGenerator.Initialize, and both emission paths then call AddSource with the same hint names:
GenerateWrapMock and GenerateSingleTypeMock both route through GenerateImplFactoryMembersAndEvents, which adds {name}_MockImplFactory.g.cs, {name}_MockMembers.g.cs and, when the type has events, {name}_MockEvents.g.cs.
The generated types do not collide — the wrap impl and factory are already named {name}WrapMockImpl / {name}WrapMockFactory and are file-scoped — but the hint names do. The
member surface ({name}_MockMemberExtensions) is identical for both models and is not file-scoped,
so emitting it twice would also be a duplicate type.
GeneratedNameCollisionDetector already recognises this situation ("Same target mocked in more than
one mode (Mock.Of and Mock.Wrap of one type, say) reaches this point as separate models sharing an
identity") and deliberately does not report TM008 for it, but nothing downstream prevents the
duplicate emission.
Expected
Both mock kinds can be generated for the same type in one compilation: one impl/factory per kind
under distinct hint names, and the shared setup/verification surface emitted once.
Activity
fix(mocks): let one type be mocked regularly and wrapped in one compi…