Skip to content

Commit 6a87751

Browse files
committed
Simplify exception assertions
1 parent bd31216 commit 6a87751

File tree

9 files changed

+28
-54
lines changed

9 files changed

+28
-54
lines changed

test/OpenApiClientTests/LegacyClient/ResponseTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,9 @@ public async Task Getting_unknown_resource_translates_error_response()
216216

217217
// Assert
218218
ExceptionAssertions<ApiException> assertion = await action.Should().ThrowExactlyAsync<ApiException>();
219-
ApiException exception = assertion.Subject.Single();
220219

221-
exception.StatusCode.Should().Be((int)HttpStatusCode.NotFound);
222-
exception.Response.Should().Be(responseBody);
220+
assertion.Which.StatusCode.Should().Be((int)HttpStatusCode.NotFound);
221+
assertion.Which.Response.Should().Be(responseBody);
223222
}
224223

225224
[Fact]

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOff/ModelStateValidationOff/CreateResourceTests.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,8 @@ public async Task Cannot_omit_attribute(string attributePropertyName, string jso
177177

178178
// Assert
179179
ExceptionAssertions<InvalidOperationException> assertion = await action.Should().ThrowExactlyAsync<InvalidOperationException>();
180-
InvalidOperationException exception = assertion.Subject.Single();
181180

182-
exception.Message.Should().Be(
181+
assertion.Which.Message.Should().Be(
183182
$"Required property '{attributePropertyName}' at JSON path 'data.attributes.{jsonPropertyName}' is not set. If sending its default value is intended, include it explicitly.");
184183
}
185184

@@ -294,9 +293,8 @@ public async Task Cannot_clear_relationship_with_partial_attribute_serialization
294293

295294
// Assert
296295
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
297-
JsonSerializationException exception = assertion.Subject.Single();
298296

299-
exception.Message.Should().Be(
297+
assertion.Which.Message.Should().Be(
300298
$"Cannot write a null value for property 'data'. Property requires a value. Path 'data.relationships.{jsonPropertyName}'.");
301299
}
302300

@@ -331,9 +329,8 @@ public async Task Cannot_clear_relationship_without_partial_attribute_serializat
331329

332330
// Assert
333331
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
334-
JsonSerializationException exception = assertion.Subject.Single();
335332

336-
exception.Message.Should().Be(
333+
assertion.Which.Message.Should().Be(
337334
$"Cannot write a null value for property 'data'. Property requires a value. Path 'data.relationships.{jsonPropertyName}'.");
338335
}
339336

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOff/ModelStateValidationOff/UpdateResourceTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ public async Task Cannot_omit_Id()
4040

4141
// Assert
4242
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
43-
JsonSerializationException exception = assertion.Subject.Single();
4443

45-
exception.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'.");
44+
assertion.Which.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'.");
4645
}
4746

4847
[Theory]

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOff/ModelStateValidationOn/CreateResourceTests.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ public async Task Cannot_clear_attribute(string attributePropertyName, string js
136136

137137
// Assert
138138
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
139-
JsonSerializationException exception = assertion.Subject.Single();
140139

141-
exception.Message.Should().Be($"Cannot write a null value for property '{jsonPropertyName}'. Property requires a value. Path 'data.attributes'.");
140+
assertion.Which.Message.Should().Be($"Cannot write a null value for property '{jsonPropertyName}'. Property requires a value. Path 'data.attributes'.");
142141
}
143142

144143
[Theory]
@@ -216,9 +215,8 @@ public async Task Cannot_omit_attribute(string attributePropertyName, string jso
216215

217216
// Assert
218217
ExceptionAssertions<InvalidOperationException> assertion = await action.Should().ThrowExactlyAsync<InvalidOperationException>();
219-
InvalidOperationException exception = assertion.Subject.Single();
220218

221-
exception.Message.Should().Be(
219+
assertion.Which.Message.Should().Be(
222220
$"Required property '{attributePropertyName}' at JSON path 'data.attributes.{jsonPropertyName}' is not set. If sending its default value is intended, include it explicitly.");
223221
}
224222

@@ -332,9 +330,8 @@ public async Task Cannot_clear_relationship_with_partial_attribute_serialization
332330

333331
// Assert
334332
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
335-
JsonSerializationException exception = assertion.Subject.Single();
336333

337-
exception.Message.Should().Be(
334+
assertion.Which.Message.Should().Be(
338335
$"Cannot write a null value for property 'data'. Property requires a value. Path 'data.relationships.{jsonPropertyName}'.");
339336
}
340337

@@ -370,9 +367,8 @@ public async Task Cannot_clear_relationship_without_partial_attribute_serializat
370367

371368
// Assert
372369
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
373-
JsonSerializationException exception = assertion.Subject.Single();
374370

375-
exception.Message.Should().Be(
371+
assertion.Which.Message.Should().Be(
376372
$"Cannot write a null value for property 'data'. Property requires a value. Path 'data.relationships.{jsonPropertyName}'.");
377373
}
378374

@@ -488,9 +484,8 @@ public async Task Cannot_omit_relationship_with_partial_attribute_serialization(
488484

489485
// Assert
490486
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
491-
JsonSerializationException exception = assertion.Subject.Single();
492487

493-
exception.Message.Should().Be(
488+
assertion.Which.Message.Should().Be(
494489
$"Cannot write a null value for property 'id'. Property requires a value. Path 'data.relationships.{jsonPropertyName}.data'.");
495490
}
496491

@@ -524,9 +519,8 @@ public async Task Cannot_omit_relationship_without_partial_attribute_serializati
524519

525520
// Assert
526521
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
527-
JsonSerializationException exception = assertion.Subject.Single();
528522

529-
exception.Message.Should().Be(
523+
assertion.Which.Message.Should().Be(
530524
$"Cannot write a null value for property 'id'. Property requires a value. Path 'data.relationships.{jsonPropertyName}.data'.");
531525
}
532526
}

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOff/ModelStateValidationOn/UpdateResourceTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ public async Task Cannot_omit_Id()
4040

4141
// Assert
4242
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
43-
JsonSerializationException exception = assertion.Subject.Single();
4443

45-
exception.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'.");
44+
assertion.Which.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'.");
4645
}
4746

4847
[Theory]

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOn/ModelStateValidationOff/CreateResourceTests.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@ public async Task Cannot_clear_attribute(string attributePropertyName, string js
144144

145145
// Assert
146146
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
147-
JsonSerializationException exception = assertion.Subject.Single();
148147

149-
exception.Message.Should().StartWith($"Cannot write a null value for property '{jsonPropertyName}'.");
150-
exception.Message.Should().EndWith("Path 'data.attributes'.");
148+
assertion.Which.Message.Should().StartWith($"Cannot write a null value for property '{jsonPropertyName}'.");
149+
assertion.Which.Message.Should().EndWith("Path 'data.attributes'.");
151150
}
152151

153152
[Theory]
@@ -231,9 +230,8 @@ public async Task Cannot_omit_attribute(string attributePropertyName, string jso
231230

232231
// Assert
233232
ExceptionAssertions<InvalidOperationException> assertion = await action.Should().ThrowExactlyAsync<InvalidOperationException>();
234-
InvalidOperationException exception = assertion.Subject.Single();
235233

236-
exception.Message.Should().Be(
234+
assertion.Which.Message.Should().Be(
237235
$"Required property '{attributePropertyName}' at JSON path 'data.attributes.{jsonPropertyName}' is not set. If sending its default value is intended, include it explicitly.");
238236
}
239237

@@ -356,9 +354,8 @@ public async Task Cannot_clear_relationship_with_partial_attribute_serialization
356354

357355
// Assert
358356
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
359-
JsonSerializationException exception = assertion.Subject.Single();
360357

361-
exception.Message.Should().Be(
358+
assertion.Which.Message.Should().Be(
362359
$"Cannot write a null value for property 'data'. Property requires a value. Path 'data.relationships.{jsonPropertyName}'.");
363360
}
364361

@@ -397,9 +394,8 @@ public async Task Cannot_clear_relationship_without_partial_attribute_serializat
397394

398395
// Assert
399396
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
400-
JsonSerializationException exception = assertion.Subject.Single();
401397

402-
exception.Message.Should().Be(
398+
assertion.Which.Message.Should().Be(
403399
$"Cannot write a null value for property 'data'. Property requires a value. Path 'data.relationships.{jsonPropertyName}'.");
404400
}
405401

@@ -521,9 +517,8 @@ public async Task Cannot_omit_relationship_with_partial_attribute_serialization(
521517

522518
// Assert
523519
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
524-
JsonSerializationException exception = assertion.Subject.Single();
525520

526-
exception.Message.Should().Be(
521+
assertion.Which.Message.Should().Be(
527522
$"Cannot write a null value for property 'id'. Property requires a value. Path 'data.relationships.{jsonPropertyName}.data'.");
528523
}
529524

@@ -559,9 +554,8 @@ public async Task Cannot_omit_relationship_without_partial_attribute_serializati
559554

560555
// Assert
561556
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
562-
JsonSerializationException exception = assertion.Subject.Single();
563557

564-
exception.Message.Should().Be(
558+
assertion.Which.Message.Should().Be(
565559
$"Cannot write a null value for property 'id'. Property requires a value. Path 'data.relationships.{jsonPropertyName}.data'.");
566560
}
567561
}

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOn/ModelStateValidationOff/UpdateResourceTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ public async Task Cannot_omit_Id()
4242

4343
// Assert
4444
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
45-
JsonSerializationException exception = assertion.Subject.Single();
4645

47-
exception.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'.");
46+
assertion.Which.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'.");
4847
}
4948

5049
[Theory]

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOn/ModelStateValidationOn/CreateResourceTests.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ public async Task Cannot_clear_attribute(string attributePropertyName, string js
144144

145145
// Assert
146146
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
147-
JsonSerializationException exception = assertion.Subject.Single();
148147

149-
exception.Message.Should().Be($"Cannot write a null value for property '{jsonPropertyName}'. Property requires a value. Path 'data.attributes'.");
148+
assertion.Which.Message.Should().Be($"Cannot write a null value for property '{jsonPropertyName}'. Property requires a value. Path 'data.attributes'.");
150149
}
151150

152151
[Theory]
@@ -230,9 +229,8 @@ public async Task Cannot_omit_attribute(string attributePropertyName, string jso
230229

231230
// Assert
232231
ExceptionAssertions<InvalidOperationException> assertion = await action.Should().ThrowExactlyAsync<InvalidOperationException>();
233-
InvalidOperationException exception = assertion.Subject.Single();
234232

235-
exception.Message.Should().Be(
233+
assertion.Which.Message.Should().Be(
236234
$"Required property '{attributePropertyName}' at JSON path 'data.attributes.{jsonPropertyName}' is not set. If sending its default value is intended, include it explicitly.");
237235
}
238236

@@ -354,9 +352,8 @@ public async Task Cannot_clear_relationship_with_partial_attribute_serialization
354352

355353
// Assert
356354
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
357-
JsonSerializationException exception = assertion.Subject.Single();
358355

359-
exception.Message.Should().Be(
356+
assertion.Which.Message.Should().Be(
360357
$"Cannot write a null value for property 'data'. Property requires a value. Path 'data.relationships.{jsonPropertyName}'.");
361358
}
362359

@@ -396,9 +393,8 @@ public async Task Cannot_clear_relationship_without_partial_attribute_serializat
396393

397394
// Assert
398395
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
399-
JsonSerializationException exception = assertion.Subject.Single();
400396

401-
exception.Message.Should().Be(
397+
assertion.Which.Message.Should().Be(
402398
$"Cannot write a null value for property 'data'. Property requires a value. Path 'data.relationships.{jsonPropertyName}'.");
403399
}
404400

@@ -522,9 +518,8 @@ public async Task Cannot_omit_relationship_with_partial_attribute_serialization(
522518

523519
// Assert
524520
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
525-
JsonSerializationException exception = assertion.Subject.Single();
526521

527-
exception.Message.Should().Be(
522+
assertion.Which.Message.Should().Be(
528523
$"Cannot write a null value for property 'id'. Property requires a value. Path 'data.relationships.{jsonPropertyName}.data'.");
529524
}
530525

@@ -562,9 +557,8 @@ public async Task Cannot_omit_relationship_without_partial_attribute_serializati
562557

563558
// Assert
564559
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
565-
JsonSerializationException exception = assertion.Subject.Single();
566560

567-
exception.Message.Should().Be(
561+
assertion.Which.Message.Should().Be(
568562
$"Cannot write a null value for property 'id'. Property requires a value. Path 'data.relationships.{jsonPropertyName}.data'.");
569563
}
570564
}

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOn/ModelStateValidationOn/UpdateResourceTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ public async Task Cannot_omit_Id()
4242

4343
// Assert
4444
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
45-
JsonSerializationException exception = assertion.Subject.Single();
4645

47-
exception.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'.");
46+
assertion.Which.Message.Should().Be("Cannot write a null value for property 'id'. Property requires a value. Path 'data'.");
4847
}
4948

5049
[Theory]

0 commit comments

Comments
 (0)