|
4 | 4 | using Setups.Controllers; |
5 | 5 | using Xunit; |
6 | 6 | using System.Collections.Generic; |
| 7 | + using System.Linq; |
7 | 8 |
|
8 | 9 | public class ModelStateBuilderTests |
9 | 10 | { |
@@ -58,5 +59,77 @@ public void WithModelStateWithErrorsObjectShouldWorkCorrectly() |
58 | 59 | .ShouldReturn() |
59 | 60 | .BadRequest(); |
60 | 61 | } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void WithoutModelStateByProvidingExistingKeyShouldWorkCorrectly() |
| 65 | + { |
| 66 | + var keyValuePair = new KeyValuePair<string, string>("Key1", "Value1"); |
| 67 | + |
| 68 | + MyController<MvcController> |
| 69 | + .Instance() |
| 70 | + .WithModelState(modelState => modelState |
| 71 | + .WithError(keyValuePair.Key, keyValuePair.Value) |
| 72 | + .WithError("PseudoRandomKey", "value")) |
| 73 | + .WithoutModelState(keyValuePair.Key) |
| 74 | + .Calling(c => c.GetModelStateKeys()) |
| 75 | + .ShouldReturn() |
| 76 | + .Ok(ok => ok |
| 77 | + .WithModelOfType<List<string>>() |
| 78 | + .Passing(keys => keys.Count == 1 && |
| 79 | + keys.Any(key => !key.Equals(keyValuePair.Key)))); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void WithoutModelStateByUsingTheBuilderShouldWorkCorrectly() |
| 84 | + { |
| 85 | + var keyValuePair = new KeyValuePair<string, string>("Key1", "Value1"); |
| 86 | + |
| 87 | + MyController<MvcController> |
| 88 | + .Instance() |
| 89 | + .WithModelState(modelState => modelState |
| 90 | + .WithError(keyValuePair.Key, keyValuePair.Value) |
| 91 | + .WithError("PseudoRandomKey", "value")) |
| 92 | + .WithoutModelState(modelState => modelState.WithoutModelState(keyValuePair.Key)) |
| 93 | + .Calling(c => c.GetModelStateKeys()) |
| 94 | + .ShouldReturn() |
| 95 | + .Ok(ok => ok |
| 96 | + .WithModelOfType<List<string>>() |
| 97 | + .Passing(keys => keys.Count == 1 && |
| 98 | + keys.Any(key => !key.Equals(keyValuePair.Key)))); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void WithoutModelStateShouldWorkCorrectly() |
| 103 | + { |
| 104 | + MyController<MvcController> |
| 105 | + .Instance() |
| 106 | + .WithModelState(modelState => modelState |
| 107 | + .WithError("PseudoRandomKey1", "value1") |
| 108 | + .WithError("PseudoRandomKey2", "value2")) |
| 109 | + .WithoutModelState() |
| 110 | + .Calling(c => c.GetModelStateKeys()) |
| 111 | + .ShouldReturn() |
| 112 | + .Ok(ok => ok |
| 113 | + .WithModelOfType<List<string>>() |
| 114 | + .Passing(keys => keys.Count == 0)); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void WithoutModelStateByRemovingNonExistingKeyShouldWorkCorrectly() |
| 119 | + { |
| 120 | + var keyValuePair = new KeyValuePair<string, string>("Key1", "Value1"); |
| 121 | + |
| 122 | + MyController<MvcController> |
| 123 | + .Instance() |
| 124 | + .WithModelState(modelState => modelState |
| 125 | + .WithError(keyValuePair.Key, keyValuePair.Value)) |
| 126 | + .WithoutModelState("NonExistingKey") |
| 127 | + .Calling(c => c.GetModelStateKeys()) |
| 128 | + .ShouldReturn() |
| 129 | + .Ok(ok => ok |
| 130 | + .WithModelOfType<List<string>>() |
| 131 | + .Passing(keys => keys.Count == 1 && |
| 132 | + keys.Any(key => key.Equals(keyValuePair.Key)))); |
| 133 | + } |
61 | 134 | } |
62 | 135 | } |
0 commit comments