4

As a precursor FYI, I'm a budding developer. I'm trying to write a test for an http POST method for a Ruby gem. From what I can understand, when you stub an http response, for instance with the Ruby WebMock gem, you're basically telling it what to post and then artificially telling it what to respond with. For example, here is the code I'm trying to test:

## githubrepo.rb

module Githubrepo

include HTTParty

def self.create(attributes)

  post = HTTParty.post(
      'https://api.github.com/user/repos',

      :headers => {
        'User-Agent' => 'Githubrepo',
        'Content-Type' => 'application/json',
        'Accept' => 'application/json'
      },

      :basic_auth => {
          :username => attributes[:username],
          :password => attributes[:password]
      },

      :body => {
          'name' => attributes[:repository],
          'description' => attributes[:description]
      }.to_json
  )

Githubrepo.parse_response_from(post, attributes[:wants_ssh])

end

My RSpec test fails when I write:

Githubrepo.create(:repository => 'test', :username => 'test_user', :password => '1234')

because it makes a real HTTP request. It recommends I do the following instead:

        stub_request(:post, "https://test_user:test_password@api.github.com/user/repos").
                with(:body => "{\"name\":\"test_repo\",\"description\":null}",
                     :headers => {'Accept'=>'application/json', 'Content-Type'=>'application/json', 'User-Agent'=>'Githubrepo'}).
           to_return(:status => 200, :body => "", :headers => {})

But to me, this seems like it's pointless since it's basically telling what to send and what to respond with. I can edit the URL to say "https://bananas@git-banana.banana" and the header to say Content-type => 'Rumplestilskin' and RSpec is ok with that. How am I supposed to integrate this into testing the functionality of the create method I specified above? Or if anything, can somebody point me to a solid beginner guide or blog to help me with this question? The Ruby gem READMEs seem to assume the user knows a thing or two already about this and I don't.

Aaron
  • 6,466
  • 7
  • 37
  • 75
  • 4
    The point isn't to test that gihub's api works, the point is to ensure your create method handles the response correctly. – SteveTurczyn Aug 14 '14 at 19:47
  • Ah, thank you. If you're meant to test how the response is parsed, how do you test to make sure you're making the proper call to the API? Since the response is artificially generated, is there a way to see that you're making the call correctly in order to generate the right response? – Aaron Aug 14 '14 at 20:38

1 Answers1

2

As Steve mentions in a comment, the point of this type of test is not to test the external API but instead that your code to handle and parse the response is correct.

As discussed in the comments to this question, check out the VCR gem for "recording" API responses to make sure your code processes them correctly: https://github.com/vcr/vcr

Mark Silverberg
  • 1,249
  • 2
  • 8
  • 21
  • Ah, thank you. If you're meant to test how the response is parsed, how do you test to make sure you're making the proper call to the API? Since the response is artificially generated, is there a way to see that you're making the call correctly in order to generate the right response? – Aaron Aug 14 '14 at 20:38
  • 1
    I'm mobile right now but take a look at the VCR gem. You can "record" the response and parse it rather than providing the response if that makes sense. – Mark Silverberg Aug 14 '14 at 20:39
  • 1
    You can certainly do a test to confirm the call to the API is correct, but you don't want to repeat that call in all your tests... it's expensive in resources and time. Plus 1 for @Skram suggestion to use VCR, which lets the call go through once and then automatically stubs out future calls by remembering the response and replaying it. It's a great gem.https://github.com/vcr/vcr – SteveTurczyn Aug 14 '14 at 21:02