seeResponseMatchesJsonType does not work with null data #2522

Closed
maxiwheat opened this Issue Nov 5, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@maxiwheat

I read the documentation here and found that we can validate the types of JSON data returned with a REST webservice, and it seems to support null as a valid type :

// This is an example found here : http://codeception.com/docs/modules/REST
$I->seeResponseIsJsonType([
     'user_id' => 'integer',
     'name' => 'string|null',
     'is_active' => 'boolean'
]);

However, using this in my tests, I get a failure when the data returned is null.

API returns :

{
    "id": 123456,
    "birthdate": null,
    "firstname": "John",
    "lastname": "Doe"
}
$I->seeResponseMatchesJsonType(array(
            'birthdate' => 'string|null'
));

I get a failed test with this message :

Fail birthdate: NULL is not of type string|null

Can this be fixed, or is there a workaround ? Leaving only stringdoes not help. I can remove the check for the birthdate, but it is not an ideal solution.

@DavertMik

This comment has been minimized.

Show comment
Hide comment
@DavertMik

DavertMik Nov 6, 2015

Member

seeResponseMatchesJsonType is pretty new feature so we plan to constantly improve it. Right now the best way to solve this issue is to help us make it better. Please send us a PR with a fix to it: https://github.com/Codeception/Codeception/blob/2.1/src/Codeception/Util/JsonType.php

It would be even better if you include a failing test into https://github.com/Codeception/Codeception/blob/2.1/tests/unit/Codeception/Util/JsonTypeTest.php

Member

DavertMik commented Nov 6, 2015

seeResponseMatchesJsonType is pretty new feature so we plan to constantly improve it. Right now the best way to solve this issue is to help us make it better. Please send us a PR with a fix to it: https://github.com/Codeception/Codeception/blob/2.1/src/Codeception/Util/JsonType.php

It would be even better if you include a failing test into https://github.com/Codeception/Codeception/blob/2.1/tests/unit/Codeception/Util/JsonTypeTest.php

@maxiwheat

This comment has been minimized.

Show comment
Hide comment
@maxiwheat

maxiwheat Nov 10, 2015

On a side note, I managed to make it work by inverting the types. string|null fails my test, but null|string passes.

On a side note, I managed to make it work by inverting the types. string|null fails my test, but null|string passes.

@DavertMik

This comment has been minimized.

Show comment
Hide comment
@DavertMik

DavertMik Nov 12, 2015

Member

Strange. I will try to reproduce and fix this issue

Member

DavertMik commented Nov 12, 2015

Strange. I will try to reproduce and fix this issue

@DavertMik DavertMik added the REST label Nov 12, 2015

@DavertMik

This comment has been minimized.

Show comment
Hide comment
@DavertMik

DavertMik Nov 12, 2015

Member

I didn't manage to reproduce it... Probably it depends on your PHP version. Which one are you using?

Member

DavertMik commented Nov 12, 2015

I didn't manage to reproduce it... Probably it depends on your PHP version. Which one are you using?

@DavertMik

This comment has been minimized.

Show comment
Hide comment
@DavertMik

DavertMik Nov 12, 2015

Member

I see, that was fixed in #2500

Member

DavertMik commented Nov 12, 2015

I see, that was fixed in #2500

@DavertMik DavertMik closed this Nov 12, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment