Skip to content

Stub api response - #1

Open
hpjaj wants to merge 3 commits into
get_meetup_data_issue_40from
stub-api-response
Open

Stub api response#1
hpjaj wants to merge 3 commits into
get_meetup_data_issue_40from
stub-api-response

Conversation

@hpjaj

@hpjaj hpjaj commented Oct 8, 2017

Copy link
Copy Markdown
Collaborator

Description of changes

Example of setting up testing for a third party API, mocking the API's response.

hpjaj added 3 commits October 7, 2017 11:00
This stubs out the HTTP call to Meetup’s API.  Instead, when the `.get` class method is called, it returns the stubbed response created in the support file.

These assertions are illustrating that the content from the support file was returned.
@hpjaj
hpjaj requested a review from leenyburger October 8, 2017 15:00

@hpjaj hpjaj left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leenyburger - Let me know if this makes sense, and what questions you have

Comment thread Gemfile
gem 'factory_girl_rails'
gem 'faker'
gem 'awesome_print', '~> 1.8'
gem 'pry-rails'

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gem does a lot of cool stuff. The reason I am adding it here is because, by default, it pretty prints things in the rails console. So for example, when you are viewing a deeply nested array of hashes, instead of printing it out all in one line, it will add in the proper indenting and line returns.

I used it to get the shape of the array in the new group_endpoint_parsed_response method.

Comment thread test/lib/meetup_test.rb

class MeetupTest < ActiveSupport::TestCase
test "some test" do
Meetup.stubs(:get).with("/pro/operationcode/groups", options).returns(group_endpoint_response)

@hpjaj hpjaj Oct 8, 2017

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll note that HTTParty's get method is in fact a class method on Meetup.

This line says:

In the tests,
whenever you are going to call that self.class.get method,
with these exact parameters: "/pro/operationcode/groups", options,
instead of using HTTParty,
just return whatever group_endpoint_response returns.

Also note the order of things here, with regards to the stub. Meaning you have to create the stub before you call the method it is stubbing out. i.e. line 6 has to precede line 8.

Comment thread test/lib/meetup_test.rb
test "some test" do
Meetup.stubs(:get).with("/pro/operationcode/groups", options).returns(group_endpoint_response)

parsed_response = Meetup.new.operationcode_data

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So since Meetup.new.operationcode_data calls self.class.get inside of it, it respects that stub you set up.

Comment thread test/lib/meetup_test.rb

parsed_response = Meetup.new.operationcode_data

assert_equal 2, parsed_response.size

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not the actual assertions you would make.

They are just here to illustrate that the stubbed data is in fact being returned, and that you can work with it.

@@ -0,0 +1,127 @@
require 'ostruct'

@hpjaj hpjaj Oct 8, 2017

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby's OpenStruct is a cool tool to have in your toolbox.

Reason we need this here is because it allows us to create an object, and then add methods to it that we can call using . notation.

So for example:

  • we create an (OpenStruct) object called response_struct
  • we then add a method/attribute to it called code
  • we set this code attribute equal to 200 (by default)

This allows us to call what is in the code attribute using . notation:

[2] pry(main)> response_struct = OpenStruct.new
=> #<OpenStruct>
[3] pry(main)> response_struct.code = 200
=> 200
[4] pry(main)> response_struct.code
=> 200

And this is exactly how the response returned from the Meetup API is structured. You are checking that it's code is 200 by calling response.code. OpenStruc allows for this.

response
end

def group_endpoint_parsed_response

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get this data, I:

  • hard coded the Meetup API key in the api_key value (do not commit this API key)
  • commented out the return_value_for response in you operationcode_data method (so that the whole response is returned)
  • called your operationcode_data in the rails console
  • since the response.parsed_response returns an array of like 20+ items, and we only need a couple for testing, I then called response.parsed_responsed.take(2)
  • this returns an array of two hashes, of the real data from the API

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

Successfully merging this pull request may close these issues.

1 participant