What is the purpose of completing an HTTP request (using WebMock pearls for example)?

As the predecessor of FYI, I am an aspiring developer. I am trying to write a test for the http POST method for a Ruby gem. From what I can understand, when you stub an HTTP response, for example with the Ruby WebMock gem, you basically tell him what to post and then artificially tell him what to respond. For example, here's 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 is making a real HTTP request. Instead, we recommend that you do the following:

        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 it seems pointless to me as it basically says what to send and what to reply. I can edit the url to say "https://bananas@git-banana.banana"

and header

to say Content-type => 'Rumplestilskin'

and RSpec is ok with that. How should I integrate this into testing the functionality of the method create

above? Or, if anything, can someone point me to a solid beginner's guide or blog to help me with this issue? The READMEs in Ruby Gemes seem to assume that the user already knows a thing or two about this, and I don't.

+3


source to share


1 answer


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



As stated in the comments on this question, check your VCR for recording API "responses" to make sure your code is handling them correctly: https://github.com/vcr/vcr

+1


source







All Articles