Moq.Analyzers

March 17, 2026 ยท View on GitHub

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

Moq.Analyzers is a Roslyn analyzer that helps you to write unit tests using the popular Moq framework. Moq.Analyzers protects you from common mistakes and warns you if something is wrong with your Moq configuration.

Analyzer rules

IDCategoryTitle
Moq1000UsageSealed classes cannot be mocked
Moq1001UsageMocked interfaces cannot have constructor parameters
Moq1002UsageParameters provided into mock do not match any existing constructors
Moq1003UsageInternal type requires InternalsVisibleTo for DynamicProxy
Moq1004UsageILogger should not be mocked
Moq1100CorrectnessCallback signature must match the signature of the mocked method
Moq1101CorrectnessSetupGet/SetupSet/SetupProperty should be used for properties, not for methods
Moq1200CorrectnessSetup should be used only for overridable members
Moq1201CorrectnessSetup of async methods should use .ReturnsAsync instance instead of .Result
Moq1202CorrectnessRaise event arguments should match the event delegate signature
Moq1203CorrectnessMethod setup should specify a return value
Moq1204CorrectnessRaises event arguments should match event signature
Moq1205CorrectnessEvent setup handler type should match event delegate type
Moq1206CorrectnessAsync method setups should use ReturnsAsync instead of Returns with async lambda
Moq1207CorrectnessSetupSequence should be used only for overridable members
Moq1208CorrectnessReturns() delegate type mismatch on async method setup
Moq1210CorrectnessVerify should be used only for overridable members
Moq1300UsageMock.As() should take interfaces only
Moq1301UsageMock.Get() should not take literals
Moq1302UsageLINQ to Mocks expression should be valid
Moq1400Best PracticeExplicitly choose a mocking behavior instead of relying on the default (Loose) behavior
Moq1410Best PracticeExplicitly set the Strict mocking behavior
Moq1420UsageRedundant Times.AtLeastOnce() specification can be removed
Moq1500Best PracticeMockRepository.Verify() should be called
Moq1600UsageProtected setup should use ItExpr matchers

See docs/rules/README.md for full documentation.

Getting started

Moq.Analyzers is installed from NuGet. Run this command for your test project(s):

dotnet add package Moq.Analyzers

NOTE: You must use a supported version of the .NET SDK.

Configuring rules

Moq.Analyzers follows existing conventions for enabling, disabling, or suppressing rules. See Suppress code analysis warnings - .NET | Microsoft Learn for documentation on how to configure rules for your project.

Contributions welcome

Moq.Analyzers continues to evolve and add new features. Any help will be appreciated. You can report issues, develop new features, improve the documentation, or do other cool stuff. See CONTRIBUTING.md.