Skip to content

seeResponseMatchesJsonType does not work with null data #2522

Closed

Description

@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.

Activity

  1. DavertMik commented on Nov 6, 2015

    @DavertMik
    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

  2. maxiwheat commented on Nov 10, 2015

    @maxiwheat
    Author

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

  3. DavertMik commented on Nov 12, 2015

    @DavertMik
    Member

    Strange. I will try to reproduce and fix this issue

  4. DavertMik commented on Nov 12, 2015

    @DavertMik
    Member

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

  5. DavertMik commented on Nov 12, 2015

    @DavertMik
    Member

    I see, that was fixed in #2500

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions