Leo Le Leo Le - 5 months ago 57
Ruby Question

RSpec takes long time to load files when using HTTParty

My application use HTTParty to send request (in order to crawl data). I write some rspec test to test the sending request service. My sending request service spec include tags to exclude when I run all spec tests in my application:

RSpec.describe SearchFlight::Jet::SearchFlightRequest do
describe 'send search ticket request to Jetstar website', http_request: true do
context 'search ticket round trip successfully' do
# initialize
it 'return status 200' do
# my expects here ...
end
end
end
end


When I run

bundle exec rspec --tag ~http_request


I took over 4 minutes to load and 1.86s for finished test with run options (exclude {:http_request=>true)

If I delete sending request service spec, the time to load file is about 3.02 seconds.

What is the different ? Why did request service spec took a long time while I excluded them ?

Answer

I would advise against calling an outside service from Rspecs. Take a look at webmock gem and try to mock the outside service call. Here's an example to mock all Amazon S3 requests in spec_helper.rb:

config.before(:each) do
    stub_request(:any, /.*s3.amazonaws.com.*/).to_return(:status => 200, :body => "", :headers => {})
end

That way you can mock the response expected in body.

As per your question, I don't see any reason why HTTParty could be so slow under Rspecs, might be related to your specific calls and/or the outside service used.

Hope this helps.

Comments