rubyunit-testingrspechttpartyfakeweb

How to write a test to specify the format of a http request in ruby?


I am writing a gem and I want to check that it is performing an http request with the parameters , headers and content that its supposed to pass. How do I write a unit test.

I am using httparty to do the request, I am also using fakeweb to test actions after the response.


Solution

  • I recommend using webmock and include the stub request that should be created:

    In your Gemfile:

    group :test do
      gem 'webmock'
    end
    

    In your spec:

    stub_request(:post, 
                 "https://external.api.com")
                 .with(:body => {:message => {:foo => 'bar'}}, 
                      :headers => {'Accept'=>'application/json'})