@@ -146,6 +146,8 @@ public async Task PatchAsync_Calls_Service()
146
146
const int id = 0 ;
147
147
var resource = new Resource ( ) ;
148
148
var serviceMock = new Mock < IUpdateService < Resource > > ( ) ;
149
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
150
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions ( ) ) ;
149
151
var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , update : serviceMock . Object ) ;
150
152
151
153
// act
@@ -157,12 +159,34 @@ public async Task PatchAsync_Calls_Service()
157
159
}
158
160
159
161
[ Fact ]
160
- public async Task PatchAsync_ModelStateInvalid ( )
162
+ public async Task PatchAsync_ModelStateInvalid_ValidateModelStateDisbled ( )
163
+ {
164
+ // arrange
165
+ const int id = 0 ;
166
+ var resource = new Resource ( ) ;
167
+ var serviceMock = new Mock < IUpdateService < Resource > > ( ) ;
168
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
169
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions { ValidateModelState = false } ) ;
170
+ var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , update : serviceMock . Object ) ;
171
+
172
+ // act
173
+ var response = await controller . PatchAsync ( id , resource ) ;
174
+
175
+ // assert
176
+ serviceMock . Verify ( m => m . UpdateAsync ( id , It . IsAny < Resource > ( ) ) , Times . Once ) ;
177
+ VerifyApplyContext ( ) ;
178
+ Assert . IsNotType < BadRequestObjectResult > ( response ) ;
179
+ }
180
+
181
+ [ Fact ]
182
+ public async Task PatchAsync_ModelStateInvalid_ValidateModelStateEnabled ( )
161
183
{
162
184
// arrange
163
185
const int id = 0 ;
164
186
var resource = new Resource ( ) ;
165
187
var serviceMock = new Mock < IUpdateService < Resource > > ( ) ;
188
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
189
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions { ValidateModelState = true } ) ;
166
190
var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , update : serviceMock . Object ) ;
167
191
controller . ModelState . AddModelError ( "Id" , "Failed Validation" ) ;
168
192
@@ -211,13 +235,34 @@ public async Task PostAsync_Calls_Service()
211
235
}
212
236
213
237
[ Fact ]
214
- public async Task PostAsync_ModelStateInvalid ( )
238
+ public async Task PostAsync_ModelStateInvalid_ValidateModelStateDisabled ( )
215
239
{
216
240
// arrange
217
241
var resource = new Resource ( ) ;
218
242
var serviceMock = new Mock < ICreateService < Resource > > ( ) ;
219
243
_jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
220
- _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions ( ) ) ;
244
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions { ValidateModelState = false } ) ;
245
+ var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , create : serviceMock . Object ) ;
246
+ serviceMock . Setup ( m => m . CreateAsync ( It . IsAny < Resource > ( ) ) ) . ReturnsAsync ( resource ) ;
247
+ controller . ControllerContext = new Microsoft . AspNetCore . Mvc . ControllerContext { HttpContext = new DefaultHttpContext ( ) } ;
248
+
249
+ // act
250
+ var response = await controller . PostAsync ( resource ) ;
251
+
252
+ // assert
253
+ serviceMock . Verify ( m => m . CreateAsync ( It . IsAny < Resource > ( ) ) , Times . Once ) ;
254
+ VerifyApplyContext ( ) ;
255
+ Assert . IsNotType < BadRequestObjectResult > ( response ) ;
256
+ }
257
+
258
+ [ Fact ]
259
+ public async Task PostAsync_ModelStateInvalid_ValidateModelStateEnabled ( )
260
+ {
261
+ // arrange
262
+ var resource = new Resource ( ) ;
263
+ var serviceMock = new Mock < ICreateService < Resource > > ( ) ;
264
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
265
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions { ValidateModelState = true } ) ;
221
266
var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , create : serviceMock . Object ) ;
222
267
controller . ModelState . AddModelError ( "Id" , "Failed Validation" ) ;
223
268
0 commit comments