You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
/// <returns><c>true</c> if a suitable constructor was found; otherwise <c>false</c>. </returns>
270
+
/// <returns>
271
+
/// <see langword="true" /> if a suitable constructor was found; otherwise <see langword="false" />.
272
+
/// If the construction method is a parenthesized lambda expression, <see langword="null" /> is returned.
273
+
/// </returns>
271
274
/// <remarks>Handles <see langword="params" /> and optional parameters.</remarks>
272
275
[SuppressMessage("Design","MA0051:Method is too long",Justification="This should be refactored; suppressing for now to enable TreatWarningsAsErrors in CI.")]
0 commit comments