1
votes

I am facing a problem in using the seeResponseMatchesJsonType command.

I have a json string with vars names and values and get error in "escrow_status" => "string|null". Variable $escrow_status is not initialized, and I expected to get null there, but get an error instead.

Hope, someone knows how to fix this issue.

2

2 Answers

1
votes

Like @Naktibalda said in the comment: you should split that assertion in two. There are many ways to do it, one is:

 $I->seeResponseJsonMatchesJsonPat('$.escrow'); //  you need JSON path module; I would expect seeResponseContainsJson could do it, but it appears that it also needs a value...
 $I->seeResponseMatchesJsonType(["escrow_status" => "string|null"]);

Another way would be to assert that the response is JSON, convert it to an array and make assertions on that array:

 $I->canSeeResponseIsJson();
 $data = json_decode($I->grabResponse());
 $I->assertArrayHasKey('escrow_status', $data); // might need the assert module

I assumed you used an actor class in your test (hence $I).

0
votes

JsonType does not support optional fields.
If the field can be missing, don't check it with JsonType.