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

ENHANCEMENT: Throw error if object called is not present within json_body #42

Closed
dmhalejr opened this issue Mar 3, 2015 · 2 comments
Closed

Comments

@dmhalejr
Copy link
Contributor

dmhalejr commented Mar 3, 2015

User Story:
As a user I'd like airborne to throw an error if object called is not present within the json_body so that I have better clarity on my malformed test

Test - POST:

  it 'should add new members or update existing members in bulk' do
    $faker_members = Faker::Internet.safe_email
    post '/members', {"members" => [{"email" => "#{$faker_members}"}]}
    $post_member_from_members = json_body[:member_id]
    puts  "#{$post_member_from_members} - Members"
    expect_status(200)
  end

Test - PUT:

  it 'should add a single member to one or more groups' do
    put "/members/#{$post_member_from_members}/groups", {}
    expect_status(200)
  end

json_body object called in test:

    $post_member_from_members = json_body[:member_id]

Actual json_body:

{
  "import_id": 1234
}

Result of puts:

 - Members

Failure on PUT:

  1) PUT - Members should add a single member to one or more groups
     Failure/Error: expect_status(200)

       expected: 200
            got: 404

       (compared using ==)
     # /Users/dhale/.rvm/gems/ruby-2.1.0/gems/airborne-0.1.13/lib/airborne/request_expectations.rb:35:in `expect_status'
     # /Users/dhale/.rvm/gems/ruby-2.1.0/gems/airborne-0.1.13/lib/airborne/request_expectations.rb:62:in `call'
     # /Users/dhale/.rvm/gems/ruby-2.1.0/gems/airborne-0.1.13/lib/airborne/request_expectations.rb:62:in `block (2 levels) in <module:RequestExpectations>'
     # ./spec/api_spec.rb:313:in `block (2 levels) in <top (required)>'
@brooklynDev
Copy link
Owner

json_body is a standard ruby hash, which, when accessed with a key that doesn't exist, returns nil. I wouldn't want to throw an error when trying to access it with a key that doesn't exist, but rather, in your test case, I would tell you to test for the response you're expecting:

  it 'should add new members or update existing members in bulk' do
    $faker_members = Faker::Internet.safe_email
    post '/members', {"members" => [{"email" => "#{$faker_members}"}]}
    $post_member_from_members = json_body[:member_id]
    expect_json_types({member_id: :integer})
    expect_status(200)
  end

This way, you'll know exactly where your issue is and what exactly is failing.

@dmhalejr
Copy link
Contributor Author

dmhalejr commented Mar 3, 2015

@brooklynDev Thanks Alex! Will do! Closing out

@dmhalejr dmhalejr closed this as completed Mar 3, 2015
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