I have API which uses FluentValidations.
I am writing integration test and wanted to assert that bad request response contains error field name and messages. I want to check for fieldname, and message and ensure they are same as returned from fluentvalidations. Am getting a json response filled with validation errors but not sure into which object should i deserialize into.
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
var result = JsonConvert.DeserializeObject<?>(await response.Content.ReadAsStringAsync());
result.Should().BeOfType<?>();
result.Should().NotBeNull();
result.Should().HaveCount(something);
sample response is fluentvalidation response
{
"Name": [
"Name is required.",
"Name length cannot be more that 255 chars"
],
"ListTypeId": [
"Invalid listtypeid"
],
"PartyRoleId": [
"Invalid partyroleid"
]
}
Given the provided JSON example a IDictionary<string,string[]> should be able to satisfy that model
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
var json = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<IDictionary<string,string[]>>(json);
result.Should().BeOfType<IDictionary<string,string[]>>();
result.Should().NotBeNull();
result.Should().HaveCount(something);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With