Having trouble with WebMock, not stubbing correctly

280 views
Skip to first unread message

Hunter Stevens

unread,
Dec 26, 2014, 5:01:45 PM12/26/14
to webmoc...@googlegroups.com
**Ruby 1.9.3, Rails 2.13.0, WebMock 1.17.4**

I am writing tests for a company app. The controller in question displays a table of a customer's placed calls, and allows for sort/filter options.

1. As a call is placed, its data joins an xml file, **created by an external API, called _Recorder_**
2. The _RecordingsController_ takes the xml file, and parses it into a hash.
3. When you visit the associated path, you see the results of the hash -- a table of placed calls, their attributes, and parameters for sort/filter.

Here is my spec so far.

require 'spec_helper'
include
Helpers

feature
'Exercise recordings controller' do
  include_context
"shared admin context"

  scenario
'show index page with empty xml' do
    canned_xml
= File.open("spec/support/assets/canned_response.xml").read
    stub_request
(:get, /localhost\:3000/).
     
with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
      to_return
(status: 200, body: canned_xml, headers: {})
    uri
= URI.parse("http://localhost:3000/recorder/index")
    visit recorded_calls_path
    page
.save_and_open_page
    expect
(title).to eq("Recorded Calls")
 
end
end


And here is the RecordingsController

class RecordingsController < ApplicationController
 
# before_filter options
 
def index
    test_session_id
= request.session_options[:id]
   
#Make request to recording app for xml of files
    uri
= URI.parse("http://#{Rails.application.config.recorder_server}:#{Rails.application.config.recorder_server_port}/recorder/index")
    http
= Net::HTTP.new(uri.host, uri.port)
    xml_request
= Net::HTTP::Post.new(uri.request_uri)
    xml_request_data
= Hash.new
   
# ... sorting params
    xml_request_data
[:shared_session_id] = request.session_options[:id]
    xml_request
.set_form_data(xml_request_data)
    response
= http.request(xml_request)
   
if response.class == Net::HTTPOK
     
@recordings_xml = XmlSimple.xml_in(response.body)
     
@recordings_sorted = @recordings_xml["Recording"].sort { |a,b| Time.parse("#{a["date"]} #{a["time"]}") <=> Time.parse("#{b["date"]} #{b["time"]}") } unless @recordings_xml["Recording"].nil?
   
else @recordings_xml = Hash.new
   
end
 
end
 
# ... other defs
end


Any and all advice is much appreciated. Thank you.

Aleksey Zapparov

unread,
Dec 28, 2014, 12:22:27 AM12/28/14
to webmoc...@googlegroups.com
Hi,

First of all, can you please, explain what exactly do you mean by "not stubbing correctly"? Answering two questions: "what is expected result in my opinion" and "what is actual result I have" will significantly help to understand your question.
Message has been deleted

Hunter Stevens

unread,
Dec 29, 2014, 8:43:37 AM12/29/14
to webmoc...@googlegroups.com
Expected response:
1. I make a stub request to an external API. This request returns a body containing a canned XML.
2. The controller being tested makes a request to the API in step 1. It reads the body (the XML), and parses it.
3. After parsing, the controller allows the view to render correctly. The view includes a table of all calls recorded (from the XML) with attributes (duration, etc).

Actual response:
A Errno::ECONNREFUSED occurred in recordings#index:
Connection refused - connect(2)
/usr/local/lib/ruby/1.9.1/net/http.rb:763:in `initialize'
-------------------------------
Request:
-------------------------------
* URL :
http://www.example.com/recorded_calls
* IP address: 127.0.0.1
* Parameters: {"controller"=>"recordings", "action"=>"index"}
* Rails root: /var/www/rails/<repository>


The test fails because the controller could not successfully make the request. Because no XML was parsed, if I were to
page.save_and_open_page
Then the page is blank -- no view is rendered.

I based my stubbing method off of a post I found on Thought Bot's blog. Is webmock the right tool to use for the tests I am writing?
It is not like I am stubbing the app I am testing, but rather an external app needed for this controller to work.
Reply all
Reply to author
Forward
0 new messages