Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integration testing api having fluent validation

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"
  ]
}
like image 781
krishnakumar Avatar asked Oct 20 '25 13:10

krishnakumar


1 Answers

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);
like image 167
Nkosi Avatar answered Oct 24 '25 20:10

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!