Code Monkey home page Code Monkey logo

moq.analyzers's Introduction

Moq.Analyzers

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

Detected issues

  • Moq1000 = Sealed classes cannot be mocked.
  • Moq1001 = Mocked interfaces cannot have constructor parameters.
  • Moq1002 = Parameters provided into mock do not match any existing constructors.
  • Moq1100 = Callback signature must match the signature of the mocked method.
  • Moq1101 = SetupGet/SetupSet should be used for properties, not for methods.
  • Moq1200 = Setup should be used only for overridable members.
  • Moq1201 = Setup of async methods should use .ReturnsAsync instance instead of .Result.
  • Moq1300 = Mock.As() should take interfaces.

How to install

Install "Moq.Analyzers" NuGet package into test projects using Moq.

Contributions are welcome!

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

If you want to contribute to existing issues, check the help wanted or good first issue items in the backlog. If you have new ideas or want to complain about bugs, feel free to create a new issue.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to set expectations for behavior in our communication. For more information, see the .NET Foundation's Contributor Convenant Code of Conduct

moq.analyzers's People

Contributors

cezarypiatek avatar litee avatar mattkotsenas avatar rjmurillo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

moq.analyzers's Issues

Settle on style guide

  • Pick a style guide and how it should be
  • Set .editconfig to match
  • Remove legacy code formatting configuration

Legacy TODO from README.md

  • Setup() should be us-ed for methods, not for properties
  • AdvancedMatcherAttribute should accept only IMatcher types
  • Checks for protected mocks when dynamically referencing their members
  • Setup() requires overridable members
  • Mock.Get() should not take literals

Rule 1200 message not helpful when developer setup includes async result

When using the result of an async operation (returning generic Task<>), rule Moq1200 fires indicating setup should only be used for overridable types. This is technically true because Task<> is abstract, however, the message is not helpful / actionable by the developer.

Example

public class AsyncClient
{
    public virtual Task<string> GenericAsyncWithConcreteReturn() => Task.FromResult(string.Empty);
}

var mock = new Mock<AsyncClient>();

// Error 1200
mock.Setup(c => c.GenericAsyncWithConcreteReturn().Result);

// Expected 
mock.Setup(c => c.GenericAsyncWithConcreteReturn())
                .ReturnsAsync(string.Empty);

Propose adding a new rule, Moq1201, that is triggered when the setup method includes a Task that calls in to .Result. The error message reflects the guidance from Moq, which is to use the .ReturnsAsync method for setup.

Set GitHub project defaults

  • Squash by default (appears to currently be set to merge)
  • Set the squashed commit message to the PR message

Convert to explicit types

There is mixed use of implicit (var) and explicit types within the code base. Since we cannot use implicit types everywhere, and using implicit types makes things difficult during refactoring, switch to explicit types.

  • Update code to explicit types
  • Set code analysis rule to prevent use of var keyword

NuGet Package does not show up as an analyzer in VS Solution Explorer

The package that is available on nuget.org (and here on GitHub) does not show up in the Visual Studio Solution explorer under "Analyzers". I think the reason for this is that the package stores the dll in \lib\netstandard1.3 instead of \analyzers\dotnet\cs
NuGetPackageExplorer_Moq Analyzers_0 0 9

I tried to investigate this a bit, but both the Diagnostic.nuspec and Moq.Analyzers.csproj look fine. Both use "analyzers\dotnet\cs" as the path. I don't know which of these two is used to built the actual nuget, my guess is Diagnostic.nuspec as it's using 0.0.9 while the csproj is still on 0.0.6.

Could you please upload an updated nuget with the correct path?

Enable warnings as errors

  • Turn on warnings as errors
  • Also cleanup <NoWarn> elements
  • Clean up warnings backlog
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>

Putting AbstractClass tests in the global namespace causes them to fail

It's unclear if this is a bug in the analyzer, bug in the test harness or something else.

However, the namespace shouldn't impact the analyzer. So once our tests are in a good spot, we should investigate.

Repro

"""
- namespace Moq.Analyzers.Test.DataAbstractClass.WithArgsPassed;
-
internal abstract class AbstractGenericClassWithCtor<T>
{

Target netstandard2.0

The analyzer targets netstandard1.3, which is no longer supported. We should target netstandard2.0 for maximum compatibility.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.