Add typed setups for IMock<T> - #1727
Merged
Merged
Conversation
…ed mock - Moq.Sdk.IMock -> IMockRuntime (untyped), IMocked.Mock -> IMocked.Runtime - DefaultMock -> DefaultMockRuntime, MockDecorator -> MockRuntimeDecorator - AsMock/Clone/InvocationsFor -> static MockRuntime.Get/TryGet/Clone/InvocationsFor - Drop Sdk IMock<T> and MockDecorator<T> - SetupScope is now reentrant (restores previous state on dispose) - IMoq/IMoq<T> -> Moq.IMock/IMock<T>, Moq<T> -> MockView<T> - Behavior/DefaultValue/CallBase/Name extension properties on IMockRuntime - Sdk extension property on IMock returning the runtime
…c Setup/SetupRef and IMock-based Verify - ISetup<TDelegate>/ISetup<TDelegate,TResult>/ISetupRef<TDelegate> expose the Sdk IMockSetup - Lazy property and indexer setup handles via SetupFactory, for generated code - Verbs as extensions on setups rather than on any T, with async unwrapping - Static Setup(() => ...) fallback with untyped argument collection handlers - Verify.Called/NotCalled/Calls on IMock<T> return verifier mocks - Fix argument matchers not applying to ref/out parameters
Extension blocks per mocked type with typed method setups, lazy property and indexer handles, delegate Invoke, RaiseXxx events and custom delegates for ref/out signatures. Mocked types are discovered from [MockGenerator] calls, object creations and class attributes, IMock<T>/Mock<T> references and mock-returning accessors like Mock.Get. Reports MOQ010 below C# 14.
Partial C# and VB classes with v4-style constructors routed through Mock.Create, an OnCreated hook, As<TInterface>() views and Mock.Get<T> overloads returning Mock<T>. The static proxy discovers mocks from [MockGenerator] constructors, and the package defaults LangVersion to latest for C# projects.
Delegate verifiers are bound back to the delegate type, recursive mocks are reused across setups, and the static proxy discovers recursive mocks through Mock.Get(Mock.Of<T>()).
MOQ001 simplifies Setup(() => mock.Object.X(...)) to the generated typed setup, MOQ002 and its code fix set up mocked members hidden by the mock (or skipped by the generator) on the mocked object, the custom delegate fix targets SetupRef, and type validation covers mock constructors and class-level [MockGenerator]. Removes the unused Moq.CodeFix and Moq.Tests.Basic projects.
…the packed Moq Static covers C# 14 typed setups on compile-time mocks, Dynamic covers the Setup(() => ...) syntax on run-time proxies under C# 8, and Dynamic.Basic covers Visual Basic. They restore Moq only from bin via their own nuget.config into an isolated packages folder. Also removes Syntax.Verify, which shadowed the Verify class under 'using static Moq.Syntax', stops leaking TypeNameFormatter.Sources to consumers, and fixes a nullable warning in the shipped Mock.cs.
Member
Author
🧪 Details on Ubuntu 24.04.5 LTSfrom retest v1.1.0 on .NET 10.0.12 with 💜 by @devlooped |
kzu
enabled auto-merge (rebase)
September 29, 2026 06:39
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.
Summary
This replaces the expression-based
Mock<T>.Setupsurface with typed setups generated for each mocked member.A setup is the member call itself:
calc.Add(2, 3).Returns(5).Syntax.Setupremains for recursive calls and for members that collide with the mock (Object,CallBase).SetupRefcovers ref/out members through a custom delegate. Verification isVerify.Called(mock)andVerify.Called(() => ...).Mock<T>and the additional-interface overloads ship as source in the package, so a test project can customize creation with a partial class. SDK introspection is nowIMockRuntime.IMock<T>is the typed mock.Across the branch
SetupFactory.Captureonce. The setup scope and the ambient setup stay behind that call. Properties, indexers, and event raising stay one call each.Returns,Callback, andThrowsconfigure the setup. A typed setup, includingSetupRef, passes the member arguments to the handler. A syntax setup (Syntax.Setup) passes the invocation's argument collection. The setup records which path produced it (ITypedSetup.IsTyped), and both verbs apply the handler in one place.Once,Never,Exactly) andVerifyrun againstIMock<T>.Mock<T>, theOf2prototype, and the hand-written mock types in tests are gone. Tests use the generated setups.Setup(() => ...)to a typed member, warn when a member hides a mocked member, and offer a custom delegate for ref/out.