-
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.
* Fix the documentation link of the PosInfoMoq2013 rule. * Add a new PosInfoMoq2014 rule to check if a delegate in the Callback() method does not a return a value (fixes: #35). * Add the PosInfoMoq2015 rule to check the return type of the Protected() setup methods (fixes #38). * Fix the PosInfoMoq1001 rule to support Mock<T> instantiation with lambda expression argument (fixes #39). * Fix the PosInfoMoq2004 to not check the Mock<T> instantiation with the factory lambda expression (fixes: #39). * Add the PosInfoMoq2016 rule to check the lambda expression factory are used only with class types (fixes: #40). * Merge the PosInfoMoq2004 rule to the ConstructorArgumentsMustMatchAnalyzer analyzer. * Rename the ConstructorArgumentsAnalyzer. * Fix the BehaviorStrict fixer with Mock.Of<T>() when using in constructors (fixes #41). * Add the PosInfoMoq1005 rule to check usage of the SetupSet<T>() method (fixes #42). * Updates the PosInfoMoq2001 to check property in the SetupSet().
- Loading branch information
1 parent
dd853ba
commit 2a5b492
Showing
34 changed files
with
1,476 additions
and
461 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
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 @@ | ||
# PosInfoMoq2014: The `Callback()` delegate expression must not return a value. | ||
|
||
| Property | Value | | ||
|-------------------------------------|-----------------------------------------------------------------------------------------| | ||
| **Rule ID** | PosInfoMoq2014 | | ||
| **Title** | The `Callback()` delegate expression must not return a value. | | ||
| **Category** | Compilation | | ||
| **Default severity** | Error | | ||
|
||
## Cause | ||
|
||
The delegate in the argument of the `Callback()` method must not return a value. | ||
|
||
## Rule description | ||
|
||
The lambda expression in the argument of the `Callback()` method must not return a value. | ||
|
||
```csharp | ||
[Fact] | ||
public void Test() | ||
{ | ||
var service = new Mock<Service>(); | ||
service.Setup(s => s.GetData("TOURREAU", 1234)) | ||
.Callback((string n, int age) => | ||
{ | ||
// ... | ||
return 1234; // The delegate in the Callback() method must not return a value. | ||
}) | ||
.Returns(10); | ||
} | ||
|
||
public interface IService | ||
{ | ||
public int GetData(string name, int age) { } | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, be sure that the delegate method in the `Callback()` method does not return a value. | ||
|
||
## 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 *"Invalid callback. This overload of the "Callback" method only accepts "void" (C#) or "Sub" (VB.NET) delegates with parameter types matching those of the set up method. (Parameter 'callback')"* 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# PosInfoMoq2015: The `Protected().Setup()` method must match the return type of the mocked method | ||
|
||
| Property | Value | | ||
|-------------------------------------|----------------------------------------------------------------------------------------------| | ||
| **Rule ID** | PosInfoMoq2015 | | ||
| **Title** | The `Protected().Setup()` method must match the return type of the mocked method. | | ||
| **Category** | Compilation | | ||
| **Default severity** | Error | | ||
|
||
## Cause | ||
|
||
The method setup with `Protected().Setup()` must match the return type of the mocked method. | ||
|
||
## Rule description | ||
|
||
When using the `Protected().Setup()`, the return type of mocked method must match of the generic | ||
argument specified in the `Setup<T>()` method. | ||
|
||
```csharp | ||
[Fact] | ||
public void Test() | ||
{ | ||
var service = new Mock<Service>(); | ||
service.Protected().Setup<int>("GetData") // OK. | ||
.Returns(10); | ||
service.Protected().Setup("GetData") // Error: The GetData() return an int, Setup<int>() must be use. | ||
service.Protected().Setup<int>("SendEmail") // Error: The SendEmail() method does not return a value, the `int` generic argument must be remove.0 | ||
.Returns(10); | ||
service.Protected().Setup<string>("GetData") // Error: The GetData() return an int, Setup<int>() must be use. | ||
.Returns("The data"); | ||
} | ||
|
||
public abstract class Service | ||
{ | ||
protected abstract int GetData(); | ||
|
||
protected abstract void SendEmail(); | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, use the generic parameter of the `Setup<T>()` method if the protected mocked | ||
method return a value. Else do not specify a generic parameter for the `Setup<T>()` method of the protected mocked | ||
method does not return a value (`void`). | ||
|
||
## 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 *"Can't set return value for void method xxx."* 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
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. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,92 @@ | ||
# PosInfoMoq1005: Defines the generic argument of the `SetupSet()` method with the type of the mocked property. | ||
|
||
| Property | Value | | ||
|-------------------------------------|------------------------------------------------------------------------------------------------------| | ||
| **Rule ID** | PosInfoMoq1005 | | ||
| **Title** | Defines the generic argument of the `SetupSet()` method with the type of the mocked property. | | ||
| **Category** | Design | | ||
| **Default severity** | Warning | | ||
|
||
## Cause | ||
|
||
A property setter has been set up using `SetupSet()` without a generic argument that represents the type of the mocked property. | ||
|
||
## Rule description | ||
|
||
Moq provides two methods to mock a property setter: | ||
- `Mock<T>.SetupSet(Action<T>)` | ||
- `Mock<T>.SetupSet<TProperty>(Action<T, TProperty>)` | ||
|
||
When setting up a property setter, use `Mock<T>.SetupSet<TProperty>(Action<T, TProperty>)` by explicitly defining the type of the property to mock. | ||
This overload of the `SetupSet()` method allows you to define a typed `Callback()` and avoid exceptions if the delegate argument in the `Callback()` | ||
does not match the property type. | ||
|
||
For example, consider the following code to test: | ||
|
||
```csharp | ||
[Fact] | ||
public interface Customer | ||
{ | ||
string Name { get; set; } | ||
} | ||
``` | ||
|
||
If you mock the setter of the `Customer.Name` property, you should set up the property with the `SetupSet<string>()` method: | ||
|
||
```csharp | ||
[Fact] | ||
public void SetNameOfCustomer() | ||
{ | ||
var customer = new Mock<Customer>(); | ||
customer.SetupSet<string>(c => c.Name = "Gilles") // The SetupSet<string>() version is used. | ||
.Callback((string value) => | ||
{ | ||
// Called when the setter of the property is set. | ||
}); | ||
} | ||
``` | ||
|
||
The following code violates the rule because the `SetupSet()` method has no generic argument: | ||
|
||
```csharp | ||
[Fact] | ||
public void SetNameOfCustomer() | ||
{ | ||
var customer = new Mock<Customer>(); | ||
customer.SetupSet(c => c.Name = "Gilles") // The SetupSet() has been used without set the generic argument. | ||
.Callback((string value) => | ||
{ | ||
// Called when the setter of the property is set. | ||
}); | ||
} | ||
``` | ||
|
||
If the non-generic version of the `SetupSet()` method is used, the delegate in the `Callback()` method cannot be checked at compile time, | ||
an exception will occur during the execution of the unit test: | ||
|
||
```csharp | ||
[Fact] | ||
public void SetNameOfCustomer() | ||
{ | ||
var customer = new Mock<Customer>(); | ||
customer.SetupSet(c => c.Name = "Gilles") | ||
.Callback((int value) => // The code compiles, but during the execution of the unit test | ||
{ // an ArgumentException will be thrown. | ||
}); | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, use the `SetupSet<TProperty>()` method with the type of the mocked property as the generic argument. | ||
|
||
### Visual Studio fixer | ||
A Visual Studio fixer exists to set explicitly the generic argument of the `SetupSet<T>()` method with the property type | ||
in the current document, project or solution. | ||
|
||
![Visual Studio rule fixer](PosInfoMoq1005-Fixer.png) | ||
|
||
## When to suppress warnings | ||
|
||
Do not suppress a warning from this rule. Using the `SetupSet<T>()` method ensures that the delegate argument in the `Callback()` | ||
method matches the type of the property. |
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
Oops, something went wrong.