|
4 | 4 |
|
5 | 5 | namespace EssentialCSharp.Web.Tests; |
6 | 6 |
|
7 | | -public class ListingSourceCodeControllerTests |
| 7 | +[ClassDataSource<WebApplicationFactory>(Shared = SharedType.PerClass)] |
| 8 | +public class ListingSourceCodeControllerTests(WebApplicationFactory factory) |
8 | 9 | { |
9 | | - [Fact] |
| 10 | + [Test] |
10 | 11 | public async Task GetListing_WithValidChapterAndListing_Returns200WithContent() |
11 | 12 | { |
12 | 13 | // Arrange |
13 | | - using WebApplicationFactory factory = new(); |
14 | 14 | HttpClient client = factory.CreateClient(); |
15 | 15 |
|
16 | 16 | // Act |
17 | 17 | using HttpResponseMessage response = await client.GetAsync("/api/ListingSourceCode/chapter/1/listing/1"); |
18 | 18 |
|
19 | 19 | // Assert |
20 | | - Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
21 | | - |
| 20 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 21 | + |
22 | 22 | ListingSourceCodeResponse? result = await response.Content.ReadFromJsonAsync<ListingSourceCodeResponse>(); |
23 | | - Assert.NotNull(result); |
24 | | - Assert.Equal(1, result.ChapterNumber); |
25 | | - Assert.Equal(1, result.ListingNumber); |
26 | | - Assert.NotEmpty(result.FileExtension); |
27 | | - Assert.NotEmpty(result.Content); |
| 23 | + await Assert.That(result).IsNotNull(); |
| 24 | + using (Assert.Multiple()) |
| 25 | + { |
| 26 | + await Assert.That(result.ChapterNumber).IsEqualTo(1); |
| 27 | + await Assert.That(result.ListingNumber).IsEqualTo(1); |
| 28 | + await Assert.That(result.FileExtension).IsNotEmpty(); |
| 29 | + await Assert.That(result.Content).IsNotEmpty(); |
| 30 | + } |
28 | 31 | } |
29 | 32 |
|
30 | 33 |
|
31 | | - [Fact] |
| 34 | + [Test] |
32 | 35 | public async Task GetListing_WithInvalidChapter_Returns404() |
33 | 36 | { |
34 | 37 | // Arrange |
35 | | - using WebApplicationFactory factory = new(); |
36 | 38 | HttpClient client = factory.CreateClient(); |
37 | 39 |
|
38 | 40 | // Act |
39 | 41 | using HttpResponseMessage response = await client.GetAsync("/api/ListingSourceCode/chapter/999/listing/1"); |
40 | 42 |
|
41 | 43 | // Assert |
42 | | - Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); |
| 44 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound); |
43 | 45 | } |
44 | 46 |
|
45 | | - [Fact] |
| 47 | + [Test] |
46 | 48 | public async Task GetListing_WithInvalidListing_Returns404() |
47 | 49 | { |
48 | 50 | // Arrange |
49 | | - using WebApplicationFactory factory = new(); |
50 | 51 | HttpClient client = factory.CreateClient(); |
51 | 52 |
|
52 | 53 | // Act |
53 | 54 | using HttpResponseMessage response = await client.GetAsync("/api/ListingSourceCode/chapter/1/listing/999"); |
54 | 55 |
|
55 | 56 | // Assert |
56 | | - Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); |
| 57 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound); |
57 | 58 | } |
58 | 59 |
|
59 | | - [Fact] |
| 60 | + [Test] |
60 | 61 | public async Task GetListingsByChapter_WithValidChapter_ReturnsMultipleListings() |
61 | 62 | { |
62 | 63 | // Arrange |
63 | | - using WebApplicationFactory factory = new(); |
64 | 64 | HttpClient client = factory.CreateClient(); |
65 | 65 |
|
66 | 66 | // Act |
67 | 67 | using HttpResponseMessage response = await client.GetAsync("/api/ListingSourceCode/chapter/1"); |
68 | 68 |
|
69 | 69 | // Assert |
70 | | - Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
71 | | - |
| 70 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 71 | + |
72 | 72 | List<ListingSourceCodeResponse>? results = await response.Content.ReadFromJsonAsync<List<ListingSourceCodeResponse>>(); |
73 | | - Assert.NotNull(results); |
74 | | - Assert.NotEmpty(results); |
75 | | - |
76 | | - // Verify all results are from chapter 1 |
77 | | - Assert.All(results, r => Assert.Equal(1, r.ChapterNumber)); |
78 | | - |
| 73 | + await Assert.That(results).IsNotNull(); |
| 74 | + await Assert.That(results).IsNotEmpty(); |
| 75 | + |
79 | 76 | // Verify results are ordered by listing number |
80 | | - Assert.Equal(results.OrderBy(r => r.ListingNumber).ToList(), results); |
81 | | - |
82 | | - // Verify each listing has required properties |
83 | | - Assert.All(results, r => |
| 77 | + await Assert.That(results).IsOrderedBy(r => r.ListingNumber); |
| 78 | + |
| 79 | + // Verify all results are from chapter 1 and have required properties |
| 80 | + foreach (var r in results) |
84 | 81 | { |
85 | | - Assert.NotEmpty(r.FileExtension); |
86 | | - Assert.NotEmpty(r.Content); |
87 | | - }); |
| 82 | + using (Assert.Multiple()) |
| 83 | + { |
| 84 | + await Assert.That(r.ChapterNumber).IsEqualTo(1); |
| 85 | + await Assert.That(r.FileExtension).IsNotEmpty(); |
| 86 | + await Assert.That(r.Content).IsNotEmpty(); |
| 87 | + } |
| 88 | + } |
88 | 89 | } |
89 | 90 |
|
90 | | - [Fact] |
| 91 | + [Test] |
91 | 92 | public async Task GetListingsByChapter_WithInvalidChapter_ReturnsEmptyList() |
92 | 93 | { |
93 | 94 | // Arrange |
94 | | - using WebApplicationFactory factory = new(); |
95 | 95 | HttpClient client = factory.CreateClient(); |
96 | 96 |
|
97 | 97 | // Act |
98 | 98 | using HttpResponseMessage response = await client.GetAsync("/api/ListingSourceCode/chapter/999"); |
99 | 99 |
|
100 | 100 | // Assert |
101 | | - Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
102 | | - |
| 101 | + await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); |
| 102 | + |
103 | 103 | List<ListingSourceCodeResponse>? results = await response.Content.ReadFromJsonAsync<List<ListingSourceCodeResponse>>(); |
104 | | - Assert.NotNull(results); |
105 | | - Assert.Empty(results); |
| 104 | + await Assert.That(results).IsNotNull(); |
| 105 | + await Assert.That(results).IsEmpty(); |
106 | 106 | } |
107 | 107 | } |
0 commit comments