1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . Linq ;
44using System . Net ;
@@ -40,10 +40,44 @@ 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+ var body = await response . Content . ReadAsStringAsync ( ) ;
74+ Assert . Equal ( 422 , Convert . ToInt32 ( response . StatusCode ) ) ;
75+ }
76+
4377 [ Fact ]
4478 public async Task Respond_404_If_EntityDoesNotExist ( )
4579 {
46- // arrange
80+ // Arrange
4781 var maxPersonId = _context . TodoItems . LastOrDefault ( ) ? . Id ?? 0 ;
4882 var todoItem = _todoItemFaker . Generate ( ) ;
4983 var builder = new WebHostBuilder ( )
@@ -65,13 +99,7 @@ public async Task Respond_404_If_EntityDoesNotExist()
6599 }
66100 }
67101 } ;
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" ) ;
102+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ maxPersonId + 100 } ", content ) ;
75103
76104 // Act
77105 var response = await client . SendAsync ( request ) ;
@@ -109,13 +137,7 @@ public async Task Can_Patch_Entity()
109137 }
110138 }
111139 } ;
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" ) ;
140+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ todoItem . Id } ", content ) ;
119141
120142 // Act
121143 var response = await client . SendAsync ( request ) ;
@@ -172,13 +194,7 @@ public async Task Patch_Entity_With_HasMany_Does_Not_Included_Relationships()
172194 }
173195 }
174196 } ;
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" ) ;
197+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/people/{ person . Id } ", content ) ;
182198
183199 // Act
184200 var response = await client . SendAsync ( request ) ;
@@ -237,13 +253,7 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
237253 }
238254 }
239255 } ;
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" ) ;
256+ var request = PrepareRequest ( "PATCH" , $ "/api/v1/todo-items/{ todoItem . Id } ", content ) ;
247257
248258 // Act
249259 var response = await client . SendAsync ( request ) ;
@@ -255,5 +265,15 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
255265 Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
256266 Assert . Equal ( person . Id , updatedTodoItem . OwnerId ) ;
257267 }
268+
269+ private HttpRequestMessage PrepareRequest ( string method , string route , object content )
270+ {
271+ var httpMethod = new HttpMethod ( method ) ;
272+ var request = new HttpRequestMessage ( httpMethod , route ) ;
273+
274+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
275+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
276+ return request ;
277+ }
258278 }
259279}
0 commit comments