-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove false positive for Moq1200 when using parameterized lambda (#301)
When using Moq's expression features for type construction, the Moq1200 analyzer was firing unexpectedly indicating a matching constructor on the type was not found. The `ConstructorArgumentsShouldMatchAnalyzer` was extracting the parameters from the Mock type and comparing them to the parameters of the constructed type as is, including the lambda and the `MockBehavior`. Example: ```csharp _ = new Mock<Calculator>(() => new Calculator(), MockBehavior.Loose); ``` > See #234 for more details This is incorrect for several reasons: 1. The parenthesized lambda is not itself a parameter for the target type, but rather the body 2. The `MockBehavior` would not likely be a parameter on the target type Correct analysis would be to drop the `MockBehavior` argument as with other configurations of the `Mock<T>` type, and use the body of the lambda. However, using the body of the lambda is not necessary. The purpose of this analyzer is to detect errors that would not be caught at compile time that would result in a runtime error. In this case, using a constructor not available on the type would result in a compiler error. As such, the constructor detection method has been updated to return a tertiary result: `true` when there is a matching constructor found, `false` when there is not, and `null` when the constructor is a lambda (i.e., the constructor should be ignored). Changes: - Add unit test for issue #234 - Add support to ignore parameterized lambda arguments when performing constructor detection - Add support `MockBehavior` definitions to be in ordinal position 0 or 1 Fixes #234
- Loading branch information
Showing
2 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
tests/Moq.Analyzers.Test/ConstructorArgumentsShouldMatchAnalyzerTests.Expressions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Verifier = Moq.Analyzers.Test.Helpers.AnalyzerVerifier<Moq.Analyzers.ConstructorArgumentsShouldMatchAnalyzer>; | ||
|
||
namespace Moq.Analyzers.Test; | ||
|
||
public partial class ConstructorArgumentsShouldMatchAnalyzerTests | ||
{ | ||
public static IEnumerable<object[]> ExpressionTestData() | ||
{ | ||
return new object[][] | ||
{ | ||
["""_ = new Mock<Calculator>(() => new Calculator(), MockBehavior.Loose);"""], | ||
["""_ = new Mock<Calculator>(() => new Calculator(), MockBehavior.Strict);"""], | ||
["""_ = new Mock<Calculator>(() => new Calculator(), MockBehavior.Default);"""], | ||
["""_ = new Mock<Calculator>(() => new Calculator());"""], | ||
}.WithNamespaces().WithMoqReferenceAssemblyGroups(); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ExpressionTestData))] | ||
public async Task ShouldPassIfExpressionWithDefaultCtorIsUsedWithMockBehavior(string referenceAssemblyGroup, string @namespace, string mock) | ||
{ | ||
await Verifier.VerifyAnalyzerAsync( | ||
$@" | ||
{@namespace} | ||
public class Calculator | ||
{{ | ||
public int Add(int a, int b) => a + b; | ||
}} | ||
internal class UnitTest | ||
{{ | ||
private void Test() | ||
{{ | ||
{mock} | ||
}} | ||
}} | ||
", | ||
referenceAssemblyGroup); | ||
} | ||
} |