Skip to content

Check the optional arguments in the Setup() methods. #57

@GillesTourreau

Description

@GillesTourreau

Category : Design
Level : Warning

Add an analyzer to check if the optional arguments in the Setup() method has been specified explicitly.

public class UnitTest1
{
    [Fact]
    public void Test1()
    {
        var mock = new Mock<ISampleInterface>();
        mock.Setup(m => m.TestMethodWithOptionalParam(It.IsAny<int>()));    // We should raise a warning for "optionalParam"
    }

    public interface ISampleInterface
    {
        void TestMethodWithOptionalParam(int requiredParam, int optionalParam = 42);
    }
}

With .NET 10.0 (C# 14.0), the optional and named arguments are supported in expression trees. Before it is raise an error from the compiler, but not is accepted by C#

Problem reported by a developer in the following devlooped/moq#1654 disussion.

Metadata

Metadata

Labels

enhancementNew feature or requestrule:designThis issue is related to design rule.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions