Skip to content

Make property setters follow Behavior when it changes after creation - #1749

Merged
kzu merged 2 commits into
mainfrom
feature/property-setters-follow-behavior
Dec 2, 2026
Merged

kzu merged 2 commits into
mainfrom
feature/property-setters-follow-behavior

Conversation

@kzu

@kzu kzu commented Dec 2, 2026

Copy link
Copy Markdown
Member

Property sets on a strict mock were rejected according to the behavior the mock was created with (PropertyBehavior.SetterRequiresSetup, set once in MockInitializer). Changing mock.Behavior afterwards changed methods and getters, but not setters:

Mock mock.Object.Name = "x" before
new Mock<T>(MockBehavior.Strict) StrictMockException
new Mock<T>(), then Behavior = Strict accepted
new Mock<T>(Strict), then Behavior = Loose still rejected

Recursive mocks and clones share the PropertyBehavior instance of their mock, so they had the same fault.

Changes

  • ConfigurePipelineBehavior reads the current Behavior on each call and skips PropertyBehavior for a set outside a setup while the mock is strict, so the set reaches StrictMockBehavior and throws.
  • PropertyBehavior.SetterRequiresSetup is removed: Moq no longer sets it.
  • Tests: ChangeBehaviorToStrictRejectsPropertySet, ChangeBehaviorToLooseAcceptsPropertySet.
  • Also includes the earlier commit Give recursive mocks the behavior of the mock they come from, which this change builds on.

Moq.Tests (net10.0) and Moq.Sdk.Tests (net48, net10.0) pass.

🤖 Generated with Claude Code

This pull request was created by CodeAlta.

kzu and others added 2 commits December 2, 2026 03:53
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 kzu added the enhancement label Dec 2, 2026
@kzu
kzu enabled auto-merge (rebase) December 2, 2026 06:54
@kzu

kzu commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

365 passed 365 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 abe6caf into main Dec 2, 2026
7 checks passed
@kzu
kzu deleted the feature/property-setters-follow-behavior branch December 2, 2026 06:55
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