55using Moq ;
66using Xunit ;
77using System . Threading . Tasks ;
8+ using JsonApiDotNetCore . Configuration ;
89using JsonApiDotNetCore . Internal ;
10+ using Microsoft . AspNetCore . Http ;
11+ using Microsoft . AspNetCore . Mvc ;
912
1013namespace UnitTests
1114{
@@ -153,6 +156,25 @@ public async Task PatchAsync_Calls_Service()
153156 VerifyApplyContext ( ) ;
154157 }
155158
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+
156178 [ Fact ]
157179 public async Task PatchAsync_Throws_405_If_No_Service ( )
158180 {
@@ -168,6 +190,46 @@ public async Task PatchAsync_Throws_405_If_No_Service()
168190 Assert . Equal ( 405 , exception . GetStatusCode ( ) ) ;
169191 }
170192
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+
171233 [ Fact ]
172234 public async Task PatchRelationshipsAsync_Calls_Service ( )
173235 {
0 commit comments