Repository navigation
Make property setters follow Behavior when it changes after creation - #1749
Merged
Merged
Conversation
A recursive mock created through a strict mock was loose. RecursiveMockBehavior added the behaviors of the mock to it, but not its state, where Behavior, CallBase and DefaultValue are kept. They are now copied when the recursive mock is created, as Moq 4 does for inner mocks. CallBase is not copied to a mock of an interface: CallBaseBehavior throws NotImplementedException for a member that has no base, instead of returning its default value. Setting DefaultValue replaces the DefaultValueBehavior of the mock instead of changing its provider, because a mock, its recursive mocks and its clones have the same behavior instances. Co-authored-by: CodeAlta <noreply@codealta.io> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The strict rule for property sets was fixed when the mock was created, through PropertyBehavior.SetterRequiresSetup. A mock made strict afterwards still accepted sets, and one made loose still rejected them; recursive mocks and clones, which share the PropertyBehavior, had the same fault. ConfigurePipelineBehavior now reads the current Behavior on each call and skips PropertyBehavior for a set outside a setup while the mock is strict. SetterRequiresSetup is removed, since nothing sets it any more. 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 06:54
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.
Property sets on a strict mock were rejected according to the behavior the mock was created with (
PropertyBehavior.SetterRequiresSetup, set once inMockInitializer). Changingmock.Behaviorafterwards changed methods and getters, but not setters:mock.Object.Name = "x"beforenew Mock<T>(MockBehavior.Strict)StrictMockExceptionnew Mock<T>(), thenBehavior = Strictnew Mock<T>(Strict), thenBehavior = LooseRecursive mocks and clones share the
PropertyBehaviorinstance of their mock, so they had the same fault.Changes
ConfigurePipelineBehaviorreads the currentBehavioron each call and skipsPropertyBehaviorfor a set outside a setup while the mock is strict, so the set reachesStrictMockBehaviorand throws.PropertyBehavior.SetterRequiresSetupis removed: Moq no longer sets it.ChangeBehaviorToStrictRejectsPropertySet,ChangeBehaviorToLooseAcceptsPropertySet.Moq.Tests (net10.0) and Moq.Sdk.Tests (net48, net10.0) pass.
🤖 Generated with Claude Code
This pull request was created by CodeAlta.