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.
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 |
Moq1100 | Correctness | Callback signature must match the signature of the mocked method |
Moq1101 | Usage | 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 |
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 |
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 |
See docs/rules/README.md for full documentation.
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 (i.e. 8.0 or later).
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.
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.