Esta página se muestra en inglés. Una traducción al español está en curso.
ScriptsJul 21, 2026·3 min de lectura

Moq — The Friendly Mocking Framework for .NET

Moq is the most popular mocking library for .NET, providing a fluent lambda-based API to create mock objects for unit testing without boilerplate or record/replay patterns.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Moq
Comando de instalación directa
npx -y tokrepo@latest install 5a178cb8-8500-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con dry-run.

Introduction

Moq leverages .NET lambda expressions to provide a clean, type-safe API for creating mock objects. Instead of generating code or using record/replay semantics, you set up expectations inline with your test logic, making tests more readable and maintainable.

What Moq Does

  • Creates mock implementations of interfaces and virtual methods at runtime
  • Sets up return values, callbacks, and sequences using lambda expressions
  • Verifies that specific methods were called with expected arguments
  • Supports loose and strict mock behavior modes
  • Handles async methods, properties, events, and generic types

Architecture Overview

Moq generates proxy classes at runtime using Castle DynamicProxy. When you call new Mock<T>(), it creates a transparent proxy that intercepts method calls and matches them against your setup expressions. The lambda-based API is parsed at compile time for type safety, while the proxy infrastructure handles dispatch at runtime. No code generation step or external tools are needed.

Self-Hosting & Configuration

  • Install via NuGet: dotnet add package Moq
  • Compatible with xUnit, NUnit, MSTest, and any .NET test framework
  • Targets .NET Standard 2.0 and .NET Standard 2.1
  • Configure default behavior with MockBehavior.Loose (default) or MockBehavior.Strict
  • Use MockRepository to batch-verify multiple mocks in a single call

Key Features

  • Type-safe lambda-based setup and verification syntax
  • Argument matchers: It.IsAny<T>(), It.Is<T>(predicate), It.IsRegex()
  • Sequential return values with SetupSequence for stateful scenarios
  • LINQ-to-Mocks for declarative mock creation
  • Protected member mocking via mock.Protected() API

Comparison with Similar Tools

  • NSubstitute — similar fluent API with slightly less ceremony; Moq has broader community adoption
  • FakeItEasy — call-configuration style; Moq uses lambda setup which some find more discoverable
  • Rhino Mocks — legacy record/replay pattern; Moq replaced it with a simpler lambda model
  • JustMock (Telerik) — commercial with free tier; can mock non-virtual and static members
  • Microsoft Fakes — Visual Studio Enterprise only; supports shims for static and sealed types

FAQ

Q: Can Moq mock concrete classes? A: Moq can mock classes with virtual methods. Non-virtual or sealed members cannot be intercepted by the runtime proxy.

Q: How do I mock async methods? A: Use ReturnsAsync() for Task-returning methods: mock.Setup(x => x.GetAsync(1)).ReturnsAsync(result);

Q: What is the difference between Loose and Strict mode? A: Loose mode returns default values for unsetup calls. Strict mode throws an exception if a call has no matching setup, helping you catch unexpected interactions.

Q: Is Moq still actively maintained? A: Yes. After a brief community concern in 2023, the project continues under active development with regular releases.

Sources

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados