Skip to content

Make mocks behave as mocks while their constructor runs - #1750

Merged
kzu merged 1 commit into
mainfrom
alta/zippy-lyric-36tz
Dec 2, 2026
Merged

kzu merged 1 commit into
mainfrom
alta/zippy-lyric-36tz

Conversation

@kzu

@kzu kzu commented Dec 2, 2026

Copy link
Copy Markdown
Member

A member that a base constructor invoked ran on an empty pipeline, because Mock.Create initialized the behaviors of a mock only after creating it. For a class whose constructor does Seen = Name():

  • Name abstract: Mock.Of<T>() and new Mock<T>() threw NotImplementedException (compiled mocks) or TargetInvocationException (Moq.DynamicProxy). The mock could not be created.
  • Name virtual: the base body ran whatever CallBase was, the call was not recorded, and a strict mock did not throw.

Mock.Create (and Mock.vb) now goes through the new MockInitializer.Create, which creates the mock inside BehaviorPipelineFactory.UseAmbient, as StuntReference.ToObject does in Stunts. Its factory returns the Moq pipeline for a mock, and the pipeline of the factory it hides for any other stunt.

  • The factory does not get the instance, so the first invocation sets the Behavior and the default value provider on the runtime. The constructor invocation ends there: it is neither recorded nor rejected.
  • Run-time proxies create their pipeline on their first intercepted call and do not send their constructor through it, so Create reads the runtime before it leaves the scope and ends the construction there.
  • A member invoked by a base constructor now returns the default value, is recorded, and throws on a strict mock, as after construction. A strict mock of a class whose constructor calls a virtual member therefore throws StrictMockException on creation.
  • CallBase cannot be set before the mock exists (new Mock<T> { CallBase = true } runs after construction), so it stays false while the constructor runs.
  • Initialize and Create share one list of behaviors.

Tests: ConstructionTests (run-time mocks) and Scenarios/Construction.cs (compiled mocks). Moq.Tests, Moq.CompiledProxy.UnitTests, Moq.Sdk.Tests and the acceptance solution against the freshly packed Moq and Moq.DynamicProxy pass on net48 and net10.0.

The compiled scenario does not mock a BCL delegate: generating Mock.Of<Func<string, int>>() fails with ST027 when the generator runs on .NET Framework, also without this change. That is tracked separately.

🤖 Generated with Claude Code

This pull request was created by CodeAlta.

Mock.Create initialized the behaviors of a mock after creating it, so a
member that a base constructor invoked ran on an empty pipeline: an
abstract one threw NotImplementedException and the mock could not be
created, and a virtual one ran its base body, unrecorded, even on a strict
mock.

Mock.Create now goes through MockInitializer.Create, which creates the mock
inside BehaviorPipelineFactory.UseAmbient, as StuntReference.ToObject does
in Stunts. Its factory returns the Moq pipeline for a mock, and the
pipeline of the factory it hides for any other stunt.

- The factory does not get the instance, so the first invocation sets the
  Behavior and the default value provider on the runtime. The constructor
  invocation ends there: it is neither recorded nor rejected.
- Run-time proxies create their pipeline on the first intercepted call and
  do not send their constructor through it, so Create reads the runtime
  before it leaves the scope and ends the construction there.
- A member invoked by a base constructor returns the default value, is
  recorded, and throws on a strict mock, as after construction.
- Initialize and Create share one list of behaviors. Mock.vb mirrors
  Mock.cs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: CodeAlta <noreply@codealta.io>
@kzu kzu added the enhancement label Dec 2, 2026
@kzu
kzu enabled auto-merge (rebase) December 2, 2026 07:24
@kzu

kzu commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

372 passed 372 passed

🧪 Details on Ubuntu 26.04.1 LTS

from retest v1.1.0 on .NET 11.0.0 with 💜 by @devlooped

@kzu
kzu merged commit bc6a002 into main Dec 2, 2026
4 checks passed
@kzu
kzu deleted the alta/zippy-lyric-36tz branch December 2, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant