# devlooped/moq architecture

> Architecture diagram of the GitHub repository devlooped/moq, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/devlooped/moq
- Repository: https://github.com/devlooped/moq

## Overview

Moq is a .NET mocking framework: test authors create mocks, configure member behavior and argument matching, exercise the mocked object, then verify recorded calls. Static source generation supplies mock implementations and typed setups; a dynamic-proxy factory is also present. The SDK runtime owns invocation state and matching behavior pipelines. The README documents typed setups, recursive and ref/out setups, events, callbacks, strict/loose behavior, and verification. Some subsystem links rely on README or SOURCE INDEX references because their implementation excerpts were not provided.

Main entry points: [`src/Moq/Syntax.cs`](https://github.com/devlooped/moq/blob/main/src/Moq/Syntax.cs) (Setup Syntax), [`src/Moq.Sdk/DefaultMockRuntime.cs`](https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/DefaultMockRuntime.cs) (Default Mock Runtime), [`src/Moq.Package/Mock.cs`](https://github.com/devlooped/moq/blob/main/src/Moq.Package/Mock.cs) (Mock Factories), [`src/Moq/SetupFactory.cs`](https://github.com/devlooped/moq/blob/main/src/Moq/SetupFactory.cs) (Setup Factory)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Mocking API"]
  node_mock_api["Mock Factories<br/>[Mock.cs]"]
  node_setup_syntax["Setup Syntax<br/>[Syntax.cs]"]
  node_setup_factory["Setup Factory<br/>[SetupFactory.cs]"]
  node_setup_contract["Setup Contract<br/>[ISetup.cs]"]
  node_matchers["Argument Matchers<br/>[AnyMatcher.cs]"]
  node_behavior_api["Behavior Extensions"]
  node_verification["Verification<br/>[Verify.cs]"]
end

subgraph group_generation["Mock Generation"]
  node_static_generator["Static Proxy Generator<br/>[MockGenerator.cs]"]
  node_proxy_emission["Proxy Emission<br/>[CSharpMocked.cs]"]
  node_typed_generator["Typed Setup Generator"]
  node_setup_renderer["Setup Renderer"]
  node_dynamic_factory["Dynamic Proxy Factory"]
end

subgraph group_runtime["Mock Runtime"]
  node_runtime["Default Mock Runtime"]
  node_runtime_contract["Runtime Contract<br/>[IMockRuntime.cs]"]
  node_setup_model["Setup Model<br/>[MockSetup.cs]"]
  node_behavior_pipeline["Behavior Pipeline"]
  node_state["Mock State<br/>[StateBag.cs]"]
end

subgraph group_tooling["IDE Tooling"]
  node_codefixes["Setup Code Fixes"]
end

node_author(("Test Author"))
node_test_project(("Test Project"))

node_author -->|"creates mock"| node_mock_api
node_mock_api -.->|"creates dynamically"| node_dynamic_factory
node_mock_api -->|"uses generated proxy"| node_static_generator
node_static_generator -->|"delegates emission"| node_proxy_emission
node_test_project -->|"compiles typed setups"| node_typed_generator
node_typed_generator -->|"renders setups"| node_setup_renderer
node_author -->|"configures members"| node_setup_syntax
node_setup_syntax -->|"captures invocation"| node_setup_factory
node_setup_factory -->|"builds setup"| node_setup_model
node_setup_syntax -->|"accepts matchers"| node_matchers
node_author -->|"sets return behavior"| node_behavior_api
node_behavior_api -->|"configures runtime"| node_runtime
node_setup_model -->|"matches invocations"| node_behavior_pipeline
node_runtime -->|"executes pipelines"| node_behavior_pipeline
node_runtime -->|"maintains state"| node_state
node_runtime -->|"implements contract"| node_runtime_contract
node_author -->|"verifies calls"| node_verification
node_verification -->|"reads invocations"| node_runtime_contract
node_author -->|"requests delegate fix"| node_codefixes
node_codefixes -->|"generates setup syntax"| node_setup_syntax
node_setup_syntax -->|"returns setup API"| node_setup_contract

click node_mock_api "https://github.com/devlooped/moq/blob/main/src/Moq.Package/Mock.cs"
click node_setup_syntax "https://github.com/devlooped/moq/blob/main/src/Moq/Syntax.cs"
click node_setup_factory "https://github.com/devlooped/moq/blob/main/src/Moq/SetupFactory.cs"
click node_setup_contract "https://github.com/devlooped/moq/blob/main/src/Moq/ISetup.cs"
click node_matchers "https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/AnyMatcher.cs"
click node_behavior_api "https://github.com/devlooped/moq/blob/main/src/Moq/ConfigurePipelineBehavior.cs"
click node_verification "https://github.com/devlooped/moq/blob/main/src/Moq/Verify.cs"
click node_static_generator "https://github.com/devlooped/moq/blob/main/src/Moq.StaticProxy/MockGenerator.cs"
click node_proxy_emission "https://github.com/devlooped/moq/blob/main/src/Moq.StaticProxy/CSharpMocked.cs"
click node_typed_generator "https://github.com/devlooped/moq/blob/main/src/Moq.CodeAnalysis/MockSetupGenerator.cs"
click node_setup_renderer "https://github.com/devlooped/moq/blob/main/src/Moq.CodeAnalysis/MockSetupRenderer.cs"
click node_dynamic_factory "https://github.com/devlooped/moq/blob/main/src/Moq.DynamicProxy/DynamicMockFactory.cs"
click node_runtime "https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/DefaultMockRuntime.cs"
click node_runtime_contract "https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/IMockRuntime.cs"
click node_setup_model "https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/MockSetup.cs"
click node_behavior_pipeline "https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/MockBehaviorPipeline.cs"
click node_state "https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/StateBag.cs"
click node_codefixes "https://github.com/devlooped/moq/blob/main/src/Moq.CodeFixes/CustomDelegateCodeFix.cs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_mock_api,node_setup_syntax,node_setup_factory,node_setup_contract,node_matchers,node_behavior_api,node_verification toneBlue
class node_static_generator,node_proxy_emission,node_typed_generator,node_setup_renderer,node_dynamic_factory toneAmber
class node_runtime,node_runtime_contract,node_setup_model,node_behavior_pipeline,node_state toneMint
class node_codefixes toneRose
class node_author,node_test_project toneIndigo
```

## Components

### Mocking API

- [Mock Factories](https://github.com/devlooped/moq/blob/main/src/Moq.Package/Mock.cs): `src/Moq.Package/Mock.cs`
- [Setup Syntax](https://github.com/devlooped/moq/blob/main/src/Moq/Syntax.cs): `src/Moq/Syntax.cs`
- [Setup Factory](https://github.com/devlooped/moq/blob/main/src/Moq/SetupFactory.cs): `src/Moq/SetupFactory.cs`
- [Setup Contract](https://github.com/devlooped/moq/blob/main/src/Moq/ISetup.cs): `src/Moq/ISetup.cs`
- [Argument Matchers](https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/AnyMatcher.cs): `src/Moq.Sdk/AnyMatcher.cs`
- [Behavior Extensions](https://github.com/devlooped/moq/blob/main/src/Moq/ConfigurePipelineBehavior.cs): `src/Moq/ConfigurePipelineBehavior.cs`
- [Verification](https://github.com/devlooped/moq/blob/main/src/Moq/Verify.cs): `src/Moq/Verify.cs`

### Mock Generation

- [Static Proxy Generator](https://github.com/devlooped/moq/blob/main/src/Moq.StaticProxy/MockGenerator.cs): `src/Moq.StaticProxy/MockGenerator.cs`
- [Proxy Emission](https://github.com/devlooped/moq/blob/main/src/Moq.StaticProxy/CSharpMocked.cs): `src/Moq.StaticProxy/CSharpMocked.cs`
- [Typed Setup Generator](https://github.com/devlooped/moq/blob/main/src/Moq.CodeAnalysis/MockSetupGenerator.cs): `src/Moq.CodeAnalysis/MockSetupGenerator.cs`
- [Setup Renderer](https://github.com/devlooped/moq/blob/main/src/Moq.CodeAnalysis/MockSetupRenderer.cs): `src/Moq.CodeAnalysis/MockSetupRenderer.cs`
- [Dynamic Proxy Factory](https://github.com/devlooped/moq/blob/main/src/Moq.DynamicProxy/DynamicMockFactory.cs): `src/Moq.DynamicProxy/DynamicMockFactory.cs`

### Mock Runtime

- [Default Mock Runtime](https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/DefaultMockRuntime.cs): `src/Moq.Sdk/DefaultMockRuntime.cs`
- [Runtime Contract](https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/IMockRuntime.cs): `src/Moq.Sdk/IMockRuntime.cs`
- [Setup Model](https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/MockSetup.cs): `src/Moq.Sdk/MockSetup.cs`
- [Behavior Pipeline](https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/MockBehaviorPipeline.cs): `src/Moq.Sdk/MockBehaviorPipeline.cs`
- [Mock State](https://github.com/devlooped/moq/blob/main/src/Moq.Sdk/StateBag.cs): `src/Moq.Sdk/StateBag.cs`

### IDE Tooling

- [Setup Code Fixes](https://github.com/devlooped/moq/blob/main/src/Moq.CodeFixes/CustomDelegateCodeFix.cs): `src/Moq.CodeFixes/CustomDelegateCodeFix.cs`

### Other components

- **Test Author**
- **Test Project**

## Connections

- Test Author → Mock Factories: creates mock
- Mock Factories → Dynamic Proxy Factory: creates dynamically (evidence: `src/Moq.Package/Mock.cs`)
- Mock Factories → Static Proxy Generator: uses generated proxy (evidence: `src/Moq.Package/Mock1.Overloads.cs`)
- Static Proxy Generator → Proxy Emission: delegates emission (evidence: `src/Moq.StaticProxy/MockGenerator.cs`)
- Test Project → Typed Setup Generator: compiles typed setups
- Typed Setup Generator → Setup Renderer: renders setups (evidence: `src/Moq.CodeAnalysis/MockSetupGenerator.cs`)
- Test Author → Setup Syntax: configures members
- Setup Syntax → Setup Factory: captures invocation (evidence: `src/Moq/Syntax.cs`)
- Setup Factory → Setup Model: builds setup (evidence: `src/Moq/SetupFactory.cs`)
- Setup Syntax → Argument Matchers: accepts matchers (evidence: `src/Moq/Syntax.cs`)
- Test Author → Behavior Extensions: sets return behavior
- Behavior Extensions → Default Mock Runtime: configures runtime (evidence: `src/Moq/ConfigurePipelineBehavior.cs`)
- Setup Model → Behavior Pipeline: matches invocations (evidence: `src/Moq.Sdk/MockBehaviorPipeline.cs`)
- Default Mock Runtime → Behavior Pipeline: executes pipelines (evidence: `src/Moq.Sdk/DefaultMockRuntime.cs`)
- Default Mock Runtime → Mock State: maintains state (evidence: `src/Moq.Sdk/DefaultMockRuntime.cs`)
- Default Mock Runtime → Runtime Contract: implements contract (evidence: `src/Moq.Sdk/DefaultMockRuntime.cs`)
- Test Author → Verification: verifies calls
- Verification → Runtime Contract: reads invocations (evidence: `src/Moq/Verify.cs`)
- Test Author → Setup Code Fixes: requests delegate fix
- Setup Code Fixes → Setup Syntax: generates setup syntax (evidence: `src/Moq.CodeFixes/CustomDelegateCodeFix.cs`)
- Setup Syntax → Setup Contract: returns setup API (evidence: `src/Moq/Syntax.cs`)
