You have problems with WebMock,

Ruby 1.9.3, RSpec 2.13.0, WebMock 1.17.4, Rails 3

I am writing tests for a company application. The controller in question displays a table of calls placed by the client and allows you to select sorting / filtering options.

EDIT The test failed because with my current setup the path is not displayed because recorder_servereither it doesnโ€™t work locally or itโ€™s not configured correctly. Also help with this.

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.recorder.example.com:8080/recorded_calls
* IP address: 127.0.0.1
* Parameters: {"controller"=>"recordings", "action"=>"index"}
* Rails root: /var/www/rails/<repository>
  • As the call is placed, its data is attached to an XML file created by an external API called Recorder
  • RecordingsController accepts the xml file and parses it for hash.
  • , - , /.

.

require 'spec_helper'
include Helpers

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

  background do
    canned_xml = File.open("spec/support/assets/canned_response.xml").read
    stub_request(:post, "http://recorder.example.com:8080/recorder/index").
      with(body: {"durations"=>["1"], "durations_greater_less"=>["gt"], "filter_from_day"=>"29", "filter_from_hour"=>"0", "filter_from_minute"=>"0", "filter_from_month"=>"12", "filter_from_year"=>"2014", "filter_prefix"=>true, "filter_to_day"=>"29", "filter_to_hour"=>"23", "filter_to_minute"=>"59", "filter_to_month"=>"12", "filter_to_year"=>"2014"}, # "shared_session_id"=>"19f9a08807cc70c1bf41885956695bde"},
           headers: {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
      to_return(status: 200, body: canned_xml, headers: {})
    uri = URI.parse("http://recorder.example.com:8080/recorder/index")
    visit recorded_calls_path
  end

  scenario 'show index page with 1 xml result' do
    #page.save_and_open_page
    expect(title).to eq("Recorded Calls")
  end
end

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

. .

+4
2

WebMock

, B-Seven . , , WebMock.

  • WebMock Gemfile group :test, :development.
    • bundle install
    • Ruby 1.9.3, Rails 2.13.0, WebMock 1.17.4
  • spec_helper.rb " HTTP-". ( backtrace, .) , , " " localhost ... , , , , .

    require 'webmock/rspec'
    WebMock.disable_net_connect!(allow_localhost: true)
    
  • test.rb recorder_server port ... , . / ( example) .

  • recordings_controller_spec.rb , XML-. , , .

    require 'spec_helper'
    include Helpers
    
    feature "Exercise recordings_controller" do
      include_context "shared admin context"
    
      # A background is currently not used, because I have 3 scenario types... No xml
      # results, 1 result, and 2 results. I will later DRY this out with a background,
      # but the heavy lifting is over, for now.
    
      scenario "show index page with 1 xml result" do
        canned_xml_1 = File.open("spec/support/assets/canned_response_1.xml").read
        stub_request(:post, "http://recorder.example.com:8080/recorder/index").
          with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
          to_return(status: 200, body: canned_xml_1, headers: {})
        uri = URI.parse("http://recorder.example.com:8080/recorder/index")
        visit recorded_calls_path
        title.should == "Recorded Calls"
        page.should have_content("Search Results")
        page.should have_content("Inbound", "5551230000", "175", "December 24 2014", "12:36:24", "134")
      end
    
    end
    

/,

+8

localhost? ,

stub_request(:get, "http://#{Rails.application.config.recorder_server}:#{Rails.application.config.recorder_server_port}/recorder/index").
+1

Source: https://habr.com/ru/post/1569086/


All Articles