Conversation
…with only parameterless constructors
18b7b25 to
fa6f0fc
Compare
There was a problem hiding this comment.
Pull request overview
This PR updates the Mockolate source generator to stop emitting public CreateMock(... constructorParameters ...) overloads for interfaces and for classes that only have a parameterless constructor, reducing the generated API surface for types that don’t need constructor forwarding.
Changes:
- Add generator tests asserting that constructor-parameters overloads are omitted for parameterless-only and implicit-ctor classes.
- Update mock class source generation to only emit constructor-parameter overloads when a type has at least one accessible parameterized constructor.
- Make the internal “core”
CreateMock(MockBehavior?, setup?, object?[]?)helper private for types without parameterized constructors.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Tests/Mockolate.SourceGenerators.Tests/MockTests.cs | Adds coverage for omitting constructor-parameter overloads on parameterless-only classes. |
| Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs | Gates constructor-parameter overload generation based on presence of accessible parameterized constructors; adjusts helper visibility accordingly. |
Comments suppressed due to low confidence (1)
Source/Mockolate.SourceGenerators/Sources/Sources.MockClass.cs:130
- By omitting the public
CreateMock(..., object?[] ...)overloads for parameterless-only classes, calls likeMyType.CreateMock(new object?[]{ })can now bind to the generic fallback overload emitted inMock.g.cs(extension<T>(T _)), which compiles but always throws at runtime. To avoid this regression, consider also preventing the fallback overload from being applicable (e.g., remove the fallback constructor-parameters overloads altogether) or emit a type-specific overload that fails at compile time (such as[Obsolete(..., error: true)]) for types where constructor parameters are not supported.
}
createMockRemarks.Add("</list>");
createMockRemarks.Add("With the default behavior, un-configured members return <c>default</c> values (empty collections / strings, completed tasks, <see langword=\"null\" /> otherwise) and base-class implementations are invoked for class mocks. Use one of the overloads that accepts a <see cref=\"global::Mockolate.MockBehavior\" /> to customize this (for example to make un-configured calls throw or to skip the base class).");
createMockRemarks.Add("Overloads allow you to additionally pass constructor parameters (for class mocks), apply an initial <c>setup</c> callback before the instance is returned, or combine both.");
sb.AppendXmlSummary(
$"Creates a new mock of <see cref=\"{escapedClassName}\" /> with the default <see cref=\"global::Mockolate.MockBehavior\" />.");
sb.AppendXmlRemarks(createMockRemarks.ToArray());
🚀 Benchmark ResultsDetails
Details
Details
Details
Details
|
|
Test Results 21 files 21 suites 7m 39s ⏱️ Results for commit fa6f0fc. |
|
This is addressed in release v3.0.0. |



This PR updates the Mockolate source generator to stop emitting public
CreateMock(... constructorParameters ...)overloads for interfaces and for classes that only have a parameterless constructor, reducing the generated API surface for types that don’t need constructor forwarding.Changes:
CreateMock(MockBehavior?, setup?, object?[]?)helper private for types without parameterized constructors.