1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . Linq ;
44using System . Net ;
@@ -40,10 +40,43 @@ public UpdatingDataTests(TestFixture<TestStartup> fixture)
4040 . RuleFor ( p => p . LastName , f => f . Name . LastName ( ) ) ;
4141 }
4242
43+
44+ [ Fact ]
45+ public async Task Response400IfUpdatingNotSettableAttribute ( )
46+ {
47+ // Arrange
48+ var builder = new WebHostBuilder ( ) . UseStartup < Startup > ( ) ;
49+ var server = new TestServer ( builder ) ;
50+ var client = server . CreateClient ( ) ;
51+
52+ var todoItem = _todoItemFaker . Generate ( ) ;
53+ _context . TodoItems . Add ( todoItem ) ;
54+ _context . SaveChanges ( ) ;
55+
56+ var content = new
57+ {
58+ datea = new
59+ {
60+ type = "todo-items" ,
61+ attributes = new
62+ {
63+ calculatedAttribute = "lol"
64+ }
65+ }
66+ } ;
67+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ todoItem . Id } ", content ) ;
68+
69+ // Act
70+ var response = await client . SendAsync ( request ) ;
71+
72+ // Assert
73+ Assert . Equal ( 422 , Convert . ToInt32 ( response . StatusCode ) ) ;
74+ }
75+
4376 [ Fact ]
4477 public async Task Respond_404_If_EntityDoesNotExist ( )
4578 {
46- // arrange
79+ // Arrange
4780 var maxPersonId = _context . TodoItems . LastOrDefault ( ) ? . Id ?? 0 ;
4881 var todoItem = _todoItemFaker . Generate ( ) ;
4982 var builder = new WebHostBuilder ( )
@@ -65,13 +98,7 @@ public async Task Respond_404_If_EntityDoesNotExist()
6598 }
6699 }
67100 } ;
68-
69- var httpMethod = new HttpMethod ( "PATCH" ) ;
70- var route = $ "/api/v1/todo-items/{ maxPersonId + 100 } ";
71- var request = new HttpRequestMessage ( httpMethod , route ) ;
72-
73- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
74- request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
101+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ maxPersonId + 100 } ", content ) ;
75102
76103 // Act
77104 var response = await client . SendAsync ( request ) ;
@@ -109,13 +136,7 @@ public async Task Can_Patch_Entity()
109136 }
110137 }
111138 } ;
112-
113- var httpMethod = new HttpMethod ( "PATCH" ) ;
114- var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
115- var request = new HttpRequestMessage ( httpMethod , route ) ;
116-
117- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
118- request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
139+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ todoItem . Id } ", content ) ;
119140
120141 // Act
121142 var response = await client . SendAsync ( request ) ;
@@ -172,13 +193,7 @@ public async Task Patch_Entity_With_HasMany_Does_Not_Included_Relationships()
172193 }
173194 }
174195 } ;
175-
176- var httpMethod = new HttpMethod ( "PATCH" ) ;
177- var route = $ "/api/v1/people/{ person . Id } ";
178- var request = new HttpRequestMessage ( httpMethod , route ) ;
179-
180- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
181- request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
196+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/people/{ person . Id } ", content ) ;
182197
183198 // Act
184199 var response = await client . SendAsync ( request ) ;
@@ -237,13 +252,7 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
237252 }
238253 }
239254 } ;
240-
241- var httpMethod = new HttpMethod ( "PATCH" ) ;
242- var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
243- var request = new HttpRequestMessage ( httpMethod , route ) ;
244-
245- request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
246- request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
255+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ todoItem . Id } ", content ) ;
247256
248257 // Act
249258 var response = await client . SendAsync ( request ) ;
@@ -255,5 +264,15 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
255264 Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
256265 Assert . Equal ( person . Id , updatedTodoItem . OwnerId ) ;
257266 }
267+
268+ private HttpRequestMessage PrepareRequest ( string method , string route , object content )
269+ {
270+ var httpMethod = new HttpMethod ( method ) ;
271+ var request = new HttpRequestMessage ( httpMethod , route ) ;
272+
273+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
274+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
275+ return request ;
276+ }
258277 }
259278}
0 commit comments