Moq.Analyzers 0.5.0-alpha

This is a prerelease version of Moq.Analyzers.
dotnet add package Moq.Analyzers --version 0.5.0-alpha
                    
NuGet\Install-Package Moq.Analyzers -Version 0.5.0-alpha
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Moq.Analyzers" Version="0.5.0-alpha">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Moq.Analyzers" Version="0.5.0-alpha" />
                    
Directory.Packages.props
<PackageReference Include="Moq.Analyzers">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Moq.Analyzers --version 0.5.0-alpha
                    
#r "nuget: Moq.Analyzers, 0.5.0-alpha"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Moq.Analyzers@0.5.0-alpha
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Moq.Analyzers&version=0.5.0-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Moq.Analyzers&version=0.5.0-alpha&prerelease
                    
Install as a Cake Tool

Moq.Analyzers

NuGet Version NuGet Downloads Main build Codacy Grade Badge Codacy Coverage Badge Ask DeepWiki

Moq.Analyzers is a set of Roslyn analyzers that help you write unit tests with the popular Moq framework. The analyzers catch common mistakes and warn you when something is wrong with your Moq configuration.

Analyzer rules

ID Category Title
Moq1000 Usage Sealed classes cannot be mocked
Moq1001 Usage Mocked interfaces cannot have constructor parameters
Moq1002 Usage Parameters provided into mock do not match any existing constructors
Moq1003 Usage Internal type requires InternalsVisibleTo for DynamicProxy
Moq1004 Usage ILogger should not be mocked
Moq1100 Correctness Callback signature must match the signature of the mocked method
Moq1101 Correctness SetupGet/SetupSet/SetupProperty should be used for properties, not for methods
Moq1200 Correctness Setup should be used only for overridable members
Moq1201 Correctness Setup of async methods should use .ReturnsAsync instance instead of .Result
Moq1202 Correctness Raise event arguments should match the event delegate signature
Moq1203 Correctness Method setup should specify a return value
Moq1204 Correctness Raises event arguments should match event signature
Moq1205 Correctness Event setup handler type should match event delegate type
Moq1206 Correctness Async method setups should use ReturnsAsync instead of Returns with async lambda
Moq1207 Correctness SetupSequence should be used only for overridable members
Moq1208 Correctness Returns() delegate type mismatch on async method setup
Moq1210 Correctness Verify should be used only for overridable members
Moq1300 Usage Mock.As() should take interfaces only
Moq1301 Usage Mock.Get() should not take literals
Moq1302 Usage LINQ to Mocks expression should be valid
Moq1400 Best Practice Explicitly choose a mocking behavior instead of relying on the default (Loose) behavior
Moq1410 Best Practice Explicitly set the Strict mocking behavior
Moq1420 Usage Redundant Times.AtLeastOnce() specification can be removed
Moq1500 Best Practice MockRepository.Verify() should be called
Moq1600 Usage Protected setup should use ItExpr matchers

For details on each rule, see the rule reference. To find the rule behind a mistake, with examples and fixes, see Common Moq mistakes. To set up async methods with ReturnsAsync, see How to set up async methods in Moq. To pass constructor arguments to a mock, see Mocking a class with constructor parameters in Moq. To capture arguments or set out values with Callback, see How to use Callback in Moq. To choose between SetupGet, SetupProperty, and Setup for a property, see Mocking properties in Moq. To check call counts, arguments, and failure messages with Verify, see Verify calls in Moq. Rules Moq1200, Moq1207, and Moq1210 treat sealed default interface members as non-overridable because Moq cannot intercept them. For analyzers that cover the rest of a test project, see Analyzers that pair well with Moq.Analyzers.

Getting started

Before you install, make sure you use a supported version of the .NET SDK. Moq.Analyzers 0.4.2 and earlier run on Roslyn 4.8.

Releases after 0.4.2 need the .NET 10 SDK (10.0.100 or later) or Visual Studio 2026, because they are built for Roslyn 5.0. Older compilers, such as the .NET 8 or .NET 9 SDK or Visual Studio 2022, skip the analyzers and report this warning (from a .NET 9.0.318 SDK build, path shortened):

warning CS9057: Analyzer assembly '<nuget-packages>/moq.analyzers/<version>/analyzers/dotnet/cs/Moq.Analyzers.dll' cannot be used because it references version '5.0.0.0' of the compiler, which is newer than the currently running version '4.14.0.0'.

If you must stay on one of those hosts, keep Moq.Analyzers 0.4.2.

To install Moq.Analyzers from NuGet, run this command in each test project:

dotnet add package Moq.Analyzers

To install in every test project at once, see Install Moq.Analyzers in every test project.

After you install the package, the analyzers report diagnostics when you build and in IDEs that support Roslyn analyzers.

Configure rules

Moq.Analyzers uses the standard .NET settings to enable, disable, or suppress rules. To configure rules for your project, see Suppress code analysis warnings on Microsoft Learn.

Contributions welcome

Moq.Analyzers keeps growing, and we welcome your help. You can report issues, build new features, or improve the documentation. To get started, see the contributing guide.

  • CI sorts each pull request into a tier based on the files it changes:
    • Docs-only pull requests skip build and test.
    • Pull requests that change rule pages or README.md build the package and run only the tests that read those files.
    • Pull requests that cannot change benchmark results, such as test-only changes, skip the perf job.
    • All other pull requests run the full pipeline.
  • When a pull request skips the full test run, CI reports the base commit's coverage to Codacy, because no measured code changed. If main CI for the base commit is still queued or running, CI waits up to 40 minutes for it to finish, and fails if that run does not pass.
  • The perf job fails only when paired reruns confirm a slowdown that the first comparison flagged. See ADR-012.
  • The tier rules live in build/scripts/ci/classify-changed-paths.sh. Any path the script does not name runs the full pipeline.
  • CI keeps artifacts from the main and mutation-testing workflows for seven days. For details, see the CI workflow requirements.
  • To learn what a pull request needs before it can merge, see the merge requirements.
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on Moq.Analyzers:

Package Downloads
Reo.Core.Testing

Package Description

Reo.Core.IntegrationTesting

Package Description

Reo.Core.CodeGeneratorTesting

Package Description

Reo.Core.Testing.Common

Package Description

Reo.Core.TUnitIntegrationTesting

Инфраструктура интеграционного тестирования на базе TUnit с общими Docker-контейнерами (Testcontainers) для всех тестов сборки

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Moq.Analyzers:

Repository Stars
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
Richasy/Rodel.Agent
支持主流在线 AI 服务的应用
Version Downloads Last Updated
0.5.0-alpha 96 11/17/2026
0.4.2 614,038 3/11/2026
0.4.2-alpha.2 182 3/7/2026
0.4.1 49,521 2/21/2026
0.4.1-alpha.3 106 2/21/2026
0.4.1-alpha.2 120 2/20/2026
0.4.1-alpha 222 2/17/2026
0.4.0 45,170 11/27/2025
0.4.0-alpha 523 11/13/2025
0.3.1 514,713 9/19/2025
0.3.0 562,020 1/21/2025
0.3.0-alpha-0001 271 1/17/2025
0.3.0-alpha 305 12/24/2024
0.2.0 71,718 10/20/2024
0.2.0-alpha 255 10/20/2024
0.1.2 127,541 8/28/2024
0.1.1 85,287 6/28/2024
0.1.0 113,574 6/4/2024
0.1.0-beta 222 6/4/2024
0.0.9 1,195,870 1/11/2022
Loading failed