コード例 #1
0
        public async Task CreateAsync_UsingValidTodoItem_ReturnsExpectedData()
        {
            // Arrange
            using HttpClient httpClient = await webApplicationFactory.CreateHttpClientWithJwtAsync();

            long?      id          = null;
            const long idThreshold = 1;

            try
            {
                var newTodoItemModel = new NewTodoItemModel
                {
                    Name       = $"it--{nameof(CreateAsync_UsingValidTodoItem_ReturnsExpectedData)}--{Guid.NewGuid():N}",
                    IsComplete = DateTime.UtcNow.Ticks % 2 == 0
                };

                // Act
                HttpResponseMessage response = await httpClient.PostAsJsonAsync(BaseUrl, newTodoItemModel);

                // Assert
                using (new AssertionScope())
                {
                    response.IsSuccessStatusCode.Should().BeTrue(BecauseAnEntityHasBeenCreated);
                    response.StatusCode.Should().Be(HttpStatusCode.Created, BecauseAnEntityHasBeenCreated);
                    response.Headers.ToDictionary(x => x.Key, x => x.Value).Should().ContainKey("Location");
                    response.Headers.Location?.OriginalString
                    .Should().MatchRegex(@"api/todo/\d+", BecauseAnEntityHasBeenCreated);

                    id = await response.Content.ReadAsAsync <long>();

                    id.Should().BeGreaterOrEqualTo(idThreshold, BecauseAnEntityHasBeenCreated);
                }
            }
            finally
            {
                if (id.HasValue)
                {
                    HttpResponseMessage response = await httpClient.DeleteAsync($"{BaseUrl}/{id}");

                    response.IsSuccessStatusCode.Should().BeTrue(BecauseCleanupMustSucceed);
                }
            }
        }