You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
assertJson() does not use strict mode, but I think it should. In my test (which tests API json response), I have a a piece of json that I expect to find like this:
$response->assertJson(['some_attribute' => 0]);
The actual response has a string value set for some_attribute. However the test succeeds, while imo it should fail, because the value was not found in the json response. The reason it does succeed is because it doesn't use strict checking. The evaluation of 0 == "some_string" is true. Strict checking would make this false.
Steps To Reproduce:
Created a simple test like this in the framework:
public function testAssertJsonZeroValue() {
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableStrictResourcesStub));
$this->expectException(ExpectationFailedException::class);
$response->assertJson(['foo' => 0]);
}
class JsonSerializableStrictResourcesStub implements JsonSerializable
{
public function jsonSerialize()
{
return [
'foo' => 'bar'
];
}
}
Possible solutions
Simply changing the strict parameter to true. I feel strict checking is appropriate. I've asked in Larachat for people to come up with why/when you really want/need non-strict checking. Haven't heard a satisfiable answer there, perhaps somewhere here can give me a scenario.
I've changed it to true, my test then passes as do all other tests.
Making strict an optional parameter for the assertJson() method, defaulting to true. This might then break tests for people.
Doing the same as option 2, only make the param default to false. This will make it not break things for people when they update.
Introduce a new method assertStrictJson(). This is not the nicest solution imo, but I've put it here, so other people can contribute. Maybe they feel this is the best choice and can come up with good arguments then.
The text was updated successfully, but these errors were encountered:
@taylorotwell I don't understand why this does not raise any discussion.
Since if I want to see 0 in my response, I do not want to see a string. Most people will not be aware of this behaviour and then have tests that succeed while they shouldn't..
This PR itself lacks tests and the documentation should be updated if this stays in like this.
Description:
assertJson() does not use strict mode, but I think it should. In my test (which tests API json response), I have a a piece of json that I expect to find like this:
The actual response has a string value set for
some_attribute
. However the test succeeds, while imo it should fail, because the value was not found in the json response. The reason it does succeed is because it doesn't use strict checking. The evaluation of0 == "some_string"
is true. Strict checking would make this false.Steps To Reproduce:
Created a simple test like this in the framework:
Possible solutions
Simply changing the strict parameter to true. I feel strict checking is appropriate. I've asked in Larachat for people to come up with why/when you really want/need non-strict checking. Haven't heard a satisfiable answer there, perhaps somewhere here can give me a scenario.
I've changed it to true, my test then passes as do all other tests.
Making strict an optional parameter for the assertJson() method, defaulting to true. This might then break tests for people.
Doing the same as option 2, only make the param default to false. This will make it not break things for people when they update.
Introduce a new method
assertStrictJson()
. This is not the nicest solution imo, but I've put it here, so other people can contribute. Maybe they feel this is the best choice and can come up with good arguments then.The text was updated successfully, but these errors were encountered: