Stub api response - #1
Conversation
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
left a comment
There was a problem hiding this comment.
@leenyburger - Let me know if this makes sense, and what questions you have
| gem 'factory_girl_rails' | ||
| gem 'faker' | ||
| gem 'awesome_print', '~> 1.8' | ||
| gem 'pry-rails' |
There was a problem hiding this comment.
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.
|
|
||
| class MeetupTest < ActiveSupport::TestCase | ||
| test "some test" do | ||
| Meetup.stubs(:get).with("/pro/operationcode/groups", options).returns(group_endpoint_response) |
There was a problem hiding this comment.
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.
| test "some test" do | ||
| Meetup.stubs(:get).with("/pro/operationcode/groups", options).returns(group_endpoint_response) | ||
|
|
||
| parsed_response = Meetup.new.operationcode_data |
There was a problem hiding this comment.
So since Meetup.new.operationcode_data calls self.class.get inside of it, it respects that stub you set up.
|
|
||
| parsed_response = Meetup.new.operationcode_data | ||
|
|
||
| assert_equal 2, parsed_response.size |
There was a problem hiding this comment.
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' | |||
There was a problem hiding this comment.
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
codeattribute equal to200(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
=> 200And 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 |
There was a problem hiding this comment.
To get this data, I:
- hard coded the Meetup API key in the
api_keyvalue (do not commit this API key) - commented out the
return_value_for responsein youoperationcode_datamethod (so that the whole response is returned) - called your
operationcode_datain therails console - since the
response.parsed_responsereturns an array of like 20+ items, and we only need a couple for testing, I then calledresponse.parsed_responsed.take(2) - this returns an array of two hashes, of the real data from the API
Description of changes
Example of setting up testing for a third party API, mocking the API's response.