Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to test for the absence of data? #126

Open
gee-forr opened this issue Feb 28, 2017 · 9 comments
Open

Is there a way to test for the absence of data? #126

gee-forr opened this issue Feb 28, 2017 · 9 comments

Comments

@gee-forr
Copy link

Hi guys,

I was just wondering if there is a way to test for the absence of JSON using airborne? My particular use case centers around me testing whether or not I am successfully sideloading some data using a toggle param, of which it has a default to not sideload under certain conditions.

Example: No sideloading

{ "foo": "bar" }

Example: sideloading

{
  "foo": "bar",
  "extraStuff": {
    "could": "be",
    "arbitrary": ["json", "data"]
  }
}

For my tests, I wish to test that in the no sideloading example, the "extraStuff" key is not there. Is this possible?

I naïvely thought !expect_json_types(extraStuff: :object) would work, but it unfortunately doesn't.

Any help or pointers would be greatly appreciated. Right now, I'm pulling the json out of json_body, and doing an expect(blob).to_not have_key :extraStuff, which works, but is... not super pretty.

@sethpollack
Copy link
Collaborator

You can adjust the strictness with match_expected: true and match_actual: true. If you do that you can just test for the presence of { "foo": "bar" } and if anything extra is there it will fail .

@sethpollack
Copy link
Collaborator

You can read more about it here https://github.com/brooklynDev/airborne#configuration

@gee-forr
Copy link
Author

gee-forr commented Mar 3, 2017

Hey @sethpollack - thanks for the response :)

I did initially go that route, but the payload in question is considerably more complex than the example I provided. It would make it quite tedious to test an entire huge payload just to ensure that one key is not there.

If it's OK with you, I'd like to issue a PR to expand the API's expect_* methods to have expect_no_* equivalents.

Sound good?

@sethpollack
Copy link
Collaborator

You can also test that it is there and expect it to fail

@gee-forr
Copy link
Author

gee-forr commented Mar 3, 2017

I'm a little slow on the uptake today :) How would I go about doing that?

@sethpollack
Copy link
Collaborator

expect { expect_json_types(name: :foo) }.to raise_error

@gee-forr
Copy link
Author

gee-forr commented Mar 3, 2017

Ah. Of course. Thanks for that.

I'd still like to issue that PR nonetheless if you're still keen?

@sethpollack
Copy link
Collaborator

What did you have in mind?

@gee-forr
Copy link
Author

gee-forr commented Mar 3, 2017

So, something like the following:

{ "foo": "bar" }

expect_no_json_types(extraStuff: :object) would pass because the extraStuff object is present.

Similarly:

expect_no_json(foo: 'baz') would also pass, as foo is bar, not baz.

The examples are tending towards the contrived, but I feel they add a lot more expressiveness than nesting an expectation and checking for an exception. It would also hew closer to how RSpec does things by providing a complementary .to_not method alongside .to.

It would work great for other matchers like expect_no_header_contains, but not so great for expect_no_json_sizes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants