0

Hi so I am setting up some Integration tests (using Xunit) and I would like to run an Assert to check whether the correct custom error message is returned.

This is the data I need to get is in the following response see image... detail: "Username must be unique" Don't worry this message will be modified to be more useful later on I am just wanting to get it working first

Required Info

This is the current code...

        //Act

        response = await _httpClient.PostAsync("CompleteUserSetup", formContent);

        //Assert

        Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode) ; //Bad request should be returned
        //TODO: check custom error message is correct

So hoping for...

ASSERT.Equal("Username must be unique", some code to get detail from response)

Dev Wood
  • 15
  • 4
  • Does this answer your question? [Getting content/message from HttpResponseMessage](https://stackoverflow.com/questions/15936180/getting-content-message-from-httpresponsemessage) – Fabio Oct 09 '21 at 21:41
  • @Fabio Hi thank you for your suggestion it wasn't exactly what I needed but lead me on to figuring it out :-D – Dev Wood Oct 11 '21 at 08:57

1 Answers1

0

Okay so I figured out how to get the data I needed. I just needed to convert the result into an object and then I was able to pull the detail data that I needed.

var resultModel = await System.Text.Json.JsonSerializer.DeserializeAsync<Result>(response.Content.ReadAsStream(), JsonSerializerHelper.DefaultDeserialisationOptions);

var errorMessage = resultModel.detail;
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Dev Wood
  • 15
  • 4