더북(TheBook)

예제 4-4 홈 리소스가 JSON을 반환하는지 확인하는 테스트
(Restaurant/316beab/Restaurant.RestApi.Tests/HomeTests.cs)

[Fact]
[SuppressMessage(
    "Usage", "CA2234:Pass system uri objects instead of strings",
    Justification = "URL isn't passed as variable, but as literal.")]
public async Task HomeReturnsJson()
{
    using var factory =  new WebApplicationFactory<Startup>();
    var client = factory.CreateClient();

    using var request =  new HttpRequestMessage(HttpMethod.Get, "");
    request.Headers.Accept.ParseAdd("application/json");
    var response =  await client.SendAsync(request);

    Assert.True(
        response.IsSuccessStatusCode,
        $"Actual status code: {response.StatusCode}.");
    Assert.Equal(
        "application/json",
        response.Content.Headers.ContentType?.MediaType);
}
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.