-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the PosInfoMoq2016 rule to check the lambda expression factory ar…
…e used only with class types (fixes: #40).
- Loading branch information
1 parent
bd80329
commit 97f6686
Showing
7 changed files
with
159 additions
and
5 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
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
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,45 @@ | ||
# PosInfoMoq2016: `Mock<T>` constructor with factory lambda expression can be used only with classes. | ||
|
||
| Property | Value | | ||
|-------------------------------------|-------------------------------------------------------------------------| | ||
| **Rule ID** | PosInfoMoq2016 | | ||
| **Title** | `Mock<T>` constructor with factory lambda expression can be used only with classes. | | ||
| **Category** | Compilation | | ||
| **Default severity** | Error | | ||
|
||
## Cause | ||
|
||
The factory lambda expression used in `Mock<T>` instantiation must used only for the classes. | ||
|
||
## Rule description | ||
|
||
When using a lambda expression in the constructor of Mock<T> to create a mock instance, the mocked type must be a class. | ||
|
||
```csharp | ||
[Fact] | ||
public void Test() | ||
{ | ||
var service1 = new Mock<IService>(() => new Service()); // The factory lambda expression can be used only on classes type. | ||
var service2 = new Mock<Service>(() => new Service()); // OK | ||
} | ||
|
||
public interface IService | ||
{ | ||
} | ||
|
||
public class Service : IService: | ||
{ | ||
public Service(string a) | ||
{ | ||
} | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, ensure that the lambda expression factory is used with a mocked type that is a class. | ||
|
||
## When to suppress warnings | ||
|
||
Do not suppress an error from this rule. If bypassed, the execution of the unit test will be failed with a `ArgumentException` | ||
thrown with the *"Constructor arguments cannot be passed for interface mocks."* message. |
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
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
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
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