Repository navigation
Make mocks behave as mocks while their constructor runs - #1750
Merged
Merged
Conversation
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
enabled auto-merge (rebase)
December 2, 2026 07:24
Member
Author
🧪 Details on Ubuntu 26.04.1 LTSfrom retest v1.1.0 on .NET 11.0.0 with 💜 by @devlooped |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A member that a base constructor invoked ran on an empty pipeline, because
Mock.Createinitialized the behaviors of a mock only after creating it. For a class whose constructor doesSeen = Name():Nameabstract:Mock.Of<T>()andnew Mock<T>()threwNotImplementedException(compiled mocks) orTargetInvocationException(Moq.DynamicProxy). The mock could not be created.Namevirtual: the base body ran whateverCallBasewas, the call was not recorded, and a strict mock did not throw.Mock.Create(andMock.vb) now goes through the newMockInitializer.Create, which creates the mock insideBehaviorPipelineFactory.UseAmbient, asStuntReference.ToObjectdoes in Stunts. Its factory returns the Moq pipeline for a mock, and the pipeline of the factory it hides for any other stunt.Behaviorand the default value provider on the runtime. The constructor invocation ends there: it is neither recorded nor rejected.Createreads the runtime before it leaves the scope and ends the construction there.StrictMockExceptionon creation.CallBasecannot be set before the mock exists (new Mock<T> { CallBase = true }runs after construction), so it stays false while the constructor runs.InitializeandCreateshare one list of behaviors.Tests:
ConstructionTests(run-time mocks) andScenarios/Construction.cs(compiled mocks).Moq.Tests,Moq.CompiledProxy.UnitTests,Moq.Sdk.Testsand the acceptance solution against the freshly packedMoqandMoq.DynamicProxypass on net48 and net10.0.The compiled scenario does not mock a BCL delegate: generating
Mock.Of<Func<string, int>>()fails withST027when 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.