简体   繁体   中英

Ruby Airbone array testing is not working as expected

I have the json below

{
    "menu": {
        "sections": [
            {
                "type": 4,
                "frames": [
                    {
                        "itens": []
                    }
                ],
                "order": 0
            },
            {
                "type": 4,
                "frames": [
                    {
                        "itens": [
                            {
                                "id": "1719016",
                                "type": 0,
                                "free": false
                            }
                        ]
                    }
                ],
                "order": 1
            }
        ]
    }
}

and the test below that may check if all json itens in array itens has an ID property:

expect_json_keys('menu.sections.0.frames.*.itens.*', :id)

The problem is that this test runs fine. But should fail.

My test only fail when I change my expectations to that:

expect_json_keys('menu.sections.0.frames.*.itens.0', :id)

Why this test is succesful instead of fail when using itens.*

I reproduced your problem and tried to debug a bit.

I see this airborne gem for the first time (so take the following with a grain of salt), but I think the problem hides in the airborne implementation itself, here, to be more precise: https://github.com/brooklynDev/airborne/blob/master/lib/airborne/path_matcher.rb#L82

This line is intended to run expectation block ( this one in this particular case) for each item matching the wildcarded segment, but for an empty array it simply does nothing. No expectations run - no failures.

So it's not something wrong in your tests code, it's about the gem itself. As a kind of workaround, you could try smth. like the following:

expect_json_types('menu.sections.0.frames.*.itens', :array_of_objects) # <= add this
expect_json_keys('menu.sections.0.frames.*.itens.*', :id)

eg testing the type of the value before testing the value itself - in this case it fails with Expected array_of_objects got Array instead

Thank you very much @konstantin-strukov. This solution works fine for this test case.

But in some test cases I still have to write some extra code.

The expectation you´ve writen fails for this json http://www.mocky.io/v2/5c827f26310000e8421d1e83 . OK, I have a test case where it should really fail. I´ll use your solution in a lot of use cases. Thank you again.

But I have some test cases that shouldn´t fail if I have at least one filled itens property ( http://www.mocky.io/v2/5c827f26310000e8421d1e83 ). expect_json_keys('menu.sections.0.frames.*.itens.?', :id) should be sufficient but it doesn´t because it works using itens.* or itens.? . I´ve tried to fit your solution in these test cases but it didn´t work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM