You should limit the amount of data in your tests so you don't have to go through a bunch of records to verify response validity. That way if you have for example two records returned you can hardcode two assertions. Or what also you can do is to assert that response is missing 'status_id' => 1 and other statuses that you don't want to see.
Jun 20, 2021
1
Level 5
API test: testing with filters
Hello,
I have the following json response of a filtered request by status_id = 2
{
"data": [
{
"id": 1,
"fund_code": "FD5-9-1",
"status_id": 2,
"reason": "Dolor est blanditiis aspernatur non laboriosam maxime.",
"amount": 112.44,
"currency": "USD",
"account_id": 5,
"requested_by": 9,
"closed_by": 2,
"rejection_reason": null,
"closed_on": 1614306465,
"created_at": 1623583853,
"updated_at": 1623583853
},
{
"id": 2,
"fund_code": "FD5-7-2",
"status_id": 3,
"reason": "Corrupti reiciendis voluptates ad aut itaque.",
"amount": 4123.4204,
"currency": "USD",
"account_id": 5,
"requested_by": 7,
"closed_by": 1,
"rejection_reason": null,
"closed_on": 1622707693,
"created_at": 1623583853,
"updated_at": 1623583853
},
{
"id": 3,
"fund_code": "FD7-1-3",
"status_id": 3,
"reason": "Debitis nulla odit voluptatem in velit cupiditate.",
"amount": 2845.394,
"currency": "USD",
"account_id": 7,
"requested_by": 1,
"closed_by": 4,
"rejection_reason": "no funds",
"closed_on": 1623786908,
"created_at": 1623583853,
"updated_at": 1623786908
}, ........ and more results
I want to test if all results status_id = 2
$filter = ['status_id' => 2];
$this->json('get', '/api/funds', $filter)
->assertJsonFragment([
'status_id' => 2
] );
the previous code only tests if status_id = 2 exist which is ok but I have to make sure that all results have status_id = 2
is there a way to do this?
note: I thought if I count number of ids and I get for example 100 id then I can count status_id = 100 then this will work. but I'm looking for a cleaner solution
Level 61
Please or to participate in this conversation.