加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 遇到WebMock问题,而不是正确存根

发布时间:2020-12-17 03:49:30 所属栏目:百科 来源:网络整理
导读:Ruby 1.9.3,RSpec 2.13.0,WebMock 1.17.4,Rails 3 我正在为公司应用编写测试.有问题的控制器显示客户已拨打电话的表格,并允许排序/过滤选项. 编辑测试失败,因为使用我当前的设置,路径不会呈现,因为recorder_server未在本地运行,或者未正确设置.请帮忙. A Err
Ruby 1.9.3,RSpec 2.13.0,WebMock 1.17.4,Rails 3

我正在为公司应用编写测试.有问题的控制器显示客户已拨打电话的表格,并允许排序/过滤选项.

编辑测试失败,因为使用我当前的设置,路径不会呈现,因为recorder_server未在本地运行,或者未正确设置.请帮忙.

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>

>随着调用的进行,其数据将加入由外部API(称为Recorder)创建的xml文件
> RecordingsController获取xml文件,并将其解析为哈希.
>当您访问关联的路径时,您会看到哈希的结果 – 已放置的调用表,它们的属性以及排序/过滤器的参数.

到目前为止,这是我的规格.

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

任何和所有建议都非常感谢.谢谢.

解决方法

我是如何配置WebMock的

我正在回答我自己的问题,在B-Seven的帮助下和一系列评论.逐个文件,我将列出为正确使用WebMock所做的更改.

>将WebMock添加到group:test,:development下的Gemfile.

> bundle install以解决依赖关系
>我目前的设置包括Ruby 1.9.3,Rails 2.13.0,WebMock 1.17.4

>设置spec_helper.rb以禁用“Real HTTP connections”. (这是在这个令人费解的过程中稍后收到的回溯错误.)根据我的理解,这允许所有“真实连接”转换为本地主机连接并脱机工作……这是很好的,因为理想情况下,我不希望外部应用程序的服务器同时运行.

require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)

>在我的test.rb环境文件中,recorder_server和port的配置已被注释掉…如果未注释,控制器将引发一个异常,说明未初始化的常量.我使用测试服务器/端口(例如替换公司名称)作为规范存根的布局.
>在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'=>'*/*',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

有帮助的建议/资源

>根据B-Seven对我原始问题的建议(参见修订版),我最初是将localhost:3000存根.他说这是不对的.经过进一步的研究,我同意,因为与WebMock的存根通常是为外部http连接保留的.
>在回答后的评论中,B-Seven列出了参考文章.我会列出那些对我帮助最大的人.

> http://robots.thoughtbot.com/how-to-stub-external-services-in-tests
> http://railscasts.com/episodes/275-how-i-test
> https://github.com/bblimke/webmock
> http://www.agileventures.org/articles/testing-with-rspec-stubs-mocks-factories-what-to-choose

>读取由错误生成的回溯非常重要.我花了这么长时间才弄清楚如何模拟主要是错误地阅读它们.从我的问题中可以看出,我正在制作一个:获取存根请求.一位同事指出,回溯建议使用:post.这是让我的规格通过的最后一块.>我决定不将配置变量作为我的存根请求输入,因为它会导致长行代码.相反,这就是我需要在test.rb中取消注释掉这些配置的原因.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读