|
| 1 | +# PosInfoMoq1005: Defines the generic argument of the `SetupSet()` method with the type of the mocked property. |
| 2 | + |
| 3 | +| Property | Value | |
| 4 | +|-------------------------------------|------------------------------------------------------------------------------------------------------| |
| 5 | +| **Rule ID** | PosInfoMoq1005 | |
| 6 | +| **Title** | Defines the generic argument of the `SetupSet()` method with the type of the mocked property. | |
| 7 | +| **Category** | Design | |
| 8 | +| **Default severity** | Warning | |
| 9 | + |
| 10 | +## Cause |
| 11 | + |
| 12 | +A property setter has been set up using `SetupSet()` without a generic argument that represents the type of the mocked property. |
| 13 | + |
| 14 | +## Rule description |
| 15 | + |
| 16 | +Moq provides two methods to mock a property setter: |
| 17 | +- `Mock<T>.SetupSet(Action<T>)` |
| 18 | +- `Mock<T>.SetupSet<TProperty>(Action<T, TProperty>)` |
| 19 | + |
| 20 | +When setting up a property setter, use `Mock<T>.SetupSet<TProperty>(Action<T, TProperty>)` by explicitly defining the type of the property to mock. |
| 21 | +This overload of the `SetupSet()` method allows you to define a typed `Callback()` and avoid exceptions if the delegate argument in the `Callback()` |
| 22 | +does not match the property type. |
| 23 | + |
| 24 | +For example, consider the following code to test: |
| 25 | + |
| 26 | +```csharp |
| 27 | +[Fact] |
| 28 | +public interface Customer |
| 29 | +{ |
| 30 | + string Name { get; set; } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +If you mock the setter of the `Customer.Name` property, you should set up the property with the `SetupSet<string>()` method: |
| 35 | + |
| 36 | +```csharp |
| 37 | +[Fact] |
| 38 | +public void SetNameOfCustomer() |
| 39 | +{ |
| 40 | + var customer = new Mock<Customer>(); |
| 41 | + customer.SetupSet<string>(c => c.Name = "Gilles") // The SetupSet<string>() version is used. |
| 42 | + .Callback((string value) => |
| 43 | + { |
| 44 | + // Called when the setter of the property is set. |
| 45 | + }); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +The following code violates the rule because the `SetupSet()` method has no generic argument: |
| 50 | + |
| 51 | +```csharp |
| 52 | +[Fact] |
| 53 | +public void SetNameOfCustomer() |
| 54 | +{ |
| 55 | + var customer = new Mock<Customer>(); |
| 56 | + customer.SetupSet(c => c.Name = "Gilles") // The SetupSet() has been used without set the generic argument. |
| 57 | + .Callback((string value) => |
| 58 | + { |
| 59 | + // Called when the setter of the property is set. |
| 60 | + }); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +If the non-generic version of the `SetupSet()` method is used, the delegate in the `Callback()` method cannot be checked at compile time, |
| 65 | +an exception will occur during the execution of the unit test: |
| 66 | + |
| 67 | +```csharp |
| 68 | +[Fact] |
| 69 | +public void SetNameOfCustomer() |
| 70 | +{ |
| 71 | + var customer = new Mock<Customer>(); |
| 72 | + customer.SetupSet(c => c.Name = "Gilles") |
| 73 | + .Callback((int value) => // The code compiles, but during the execution of the unit test |
| 74 | + { // an ArgumentException will be thrown. |
| 75 | + }); |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## How to fix violations |
| 80 | + |
| 81 | +To fix a violation of this rule, use the `SetupSet<TProperty>()` method with the type of the mocked property as the generic argument. |
| 82 | + |
| 83 | +### Visual Studio fixer |
| 84 | +A Visual Studio fixer exists to set explicitly the generic argument of the `SetupSet<T>()` method with the property type |
| 85 | +in the current document, project or solution. |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +## When to suppress warnings |
| 90 | + |
| 91 | +Do not suppress a warning from this rule. Using the `SetupSet<T>()` method ensures that the delegate argument in the `Callback()` |
| 92 | +method matches the type of the property. |
0 commit comments