5
5
using Moq ;
6
6
using Xunit ;
7
7
using System . Threading . Tasks ;
8
+ using JsonApiDotNetCore . Configuration ;
8
9
using JsonApiDotNetCore . Internal ;
10
+ using Microsoft . AspNetCore . Http ;
11
+ using Microsoft . AspNetCore . Mvc ;
9
12
10
13
namespace UnitTests
11
14
{
@@ -153,6 +156,25 @@ public async Task PatchAsync_Calls_Service()
153
156
VerifyApplyContext ( ) ;
154
157
}
155
158
159
+ [ Fact ]
160
+ public async Task PatchAsync_ModelStateInvalid ( )
161
+ {
162
+ // arrange
163
+ const int id = 0 ;
164
+ var resource = new Resource ( ) ;
165
+ var serviceMock = new Mock < IUpdateService < Resource > > ( ) ;
166
+ var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , update : serviceMock . Object ) ;
167
+ controller . ModelState . AddModelError ( "Id" , "Failed Validation" ) ;
168
+
169
+ // act
170
+ var response = await controller . PatchAsync ( id , resource ) ;
171
+
172
+ // assert
173
+ serviceMock . Verify ( m => m . UpdateAsync ( id , It . IsAny < Resource > ( ) ) , Times . Never ) ;
174
+ Assert . IsType < BadRequestObjectResult > ( response ) ;
175
+ Assert . IsType < ErrorCollection > ( ( ( BadRequestObjectResult ) response ) . Value ) ;
176
+ }
177
+
156
178
[ Fact ]
157
179
public async Task PatchAsync_Throws_405_If_No_Service ( )
158
180
{
@@ -168,6 +190,46 @@ public async Task PatchAsync_Throws_405_If_No_Service()
168
190
Assert . Equal ( 405 , exception . GetStatusCode ( ) ) ;
169
191
}
170
192
193
+ [ Fact ]
194
+ public async Task PostAsync_Calls_Service ( )
195
+ {
196
+ // arrange
197
+ var resource = new Resource ( ) ;
198
+ var serviceMock = new Mock < ICreateService < Resource > > ( ) ;
199
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
200
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions ( ) ) ;
201
+ var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , create : serviceMock . Object ) ;
202
+ serviceMock . Setup ( m => m . CreateAsync ( It . IsAny < Resource > ( ) ) ) . ReturnsAsync ( resource ) ;
203
+ controller . ControllerContext = new Microsoft . AspNetCore . Mvc . ControllerContext { HttpContext = new DefaultHttpContext ( ) } ;
204
+
205
+ // act
206
+ await controller . PostAsync ( resource ) ;
207
+
208
+ // assert
209
+ serviceMock . Verify ( m => m . CreateAsync ( It . IsAny < Resource > ( ) ) , Times . Once ) ;
210
+ VerifyApplyContext ( ) ;
211
+ }
212
+
213
+ [ Fact ]
214
+ public async Task PostAsync_ModelStateInvalid ( )
215
+ {
216
+ // arrange
217
+ var resource = new Resource ( ) ;
218
+ var serviceMock = new Mock < ICreateService < Resource > > ( ) ;
219
+ _jsonApiContextMock . Setup ( a => a . ApplyContext < Resource > ( It . IsAny < BaseJsonApiController < Resource > > ( ) ) ) . Returns ( _jsonApiContextMock . Object ) ;
220
+ _jsonApiContextMock . SetupGet ( a => a . Options ) . Returns ( new JsonApiOptions ( ) ) ;
221
+ var controller = new BaseJsonApiController < Resource > ( _jsonApiContextMock . Object , create : serviceMock . Object ) ;
222
+ controller . ModelState . AddModelError ( "Id" , "Failed Validation" ) ;
223
+
224
+ // act
225
+ var response = await controller . PostAsync ( resource ) ;
226
+
227
+ // assert
228
+ serviceMock . Verify ( m => m . CreateAsync ( It . IsAny < Resource > ( ) ) , Times . Never ) ;
229
+ Assert . IsType < BadRequestObjectResult > ( response ) ;
230
+ Assert . IsType < ErrorCollection > ( ( ( BadRequestObjectResult ) response ) . Value ) ;
231
+ }
232
+
171
233
[ Fact ]
172
234
public async Task PatchRelationshipsAsync_Calls_Service ( )
173
235
{
0 commit comments