Net :: HTTP请求中的参数数量错误

时间:2016-06-22 18:43:46

标签: ruby http rspec sinatra webmock

在Sinatra应用程序中,我正在尝试围绕Net::HTTP创建自己的包装器,但我仍然坚持我的一个测试。我正在使用Webmock存根请求并将其传递给request Net::HTTP方法。我用RSpec进行测试。我得到的错误是:

 Failure/Error: client.request(request)

 ArgumentError:
   wrong number of arguments (given 0, expected 1)
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/webmock-2.1.0/lib/webmock/http_lib_adapters/net_http.rb:279:in `method'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/webmock-2.1.0/lib/webmock/http_lib_adapters/net_http.rb:279:in `request_signature_from_request'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/webmock-2.1.0/lib/webmock/http_lib_adapters/net_http.rb:75:in `request'
 # ./lib/net_http.rb:19:in `request'
 # ./spec/net_http_spec.rb:21:in `block (2 levels) in <top (required)>'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:236:in `instance_exec'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:236:in `block in run'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:478:in `block in with_around_and_singleton_context_hooks'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:435:in `block in with_around_example_hooks'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:478:in `block in run'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:616:in `run_around_example_hooks_for'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:478:in `run'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:435:in `with_around_example_hooks'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:478:in `with_around_and_singleton_context_hooks'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:233:in `run'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:581:in `block in run_examples'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:577:in `map'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:577:in `run_examples'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:543:in `run'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:119:in `block (3 levels) in run_specs'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:119:in `map'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:119:in `block (2 levels) in run_specs'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1680:in `with_suite_hooks'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:118:in `block in run_specs'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/reporter.rb:77:in `report'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:117:in `run_specs'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:93:in `run'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
 # /home/admin/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
 # /home/admin/.rbenv/versions/2.3.0/bin/rspec:23:in `load'
 # /home/admin/.rbenv/versions/2.3.0/bin/rspec:23:in `<main>'

net_http.rb的第279行(堆栈的第一行)是:

method = request.method.downcase.to_sym

位于方法内:

module WebMock
  module NetHTTPUtility

    def self.request_signature_from_request(net_http, request, body = nil)
      ...

在同一文件的第75行中调用:

def request(request, body = nil, &block)
  request_signature = WebMock::NetHTTPUtility.request_signature_from_request(self, request, body)

这是我在客户端上调用的方法。这些是我的文件:

net_http.rb

class NetHttp

  def initialize(client)
    @client = client
  end

  def request(request)
    client.request(request)
  end

  private

  attr_reader :client

end

net_http_spec.rb

describe "NetHttp" do

  let(:client)   {Net::HTTP.new(VERIFY_URL.host, VERIFY_URL.port)}
  let(:net_http) {NetHttp.new(client)}

  it "sends a get request" do
    request  = request_with_path
    response = net_http.request(request)
    expect(response.code).to eq("200")
  end

  def request_with_path
    request = stub_request(:get, VERIFY_URL)
      .with(:headers => {
        'Accept'=>'*/*',
        'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
        'User-Agent'=>'Ruby'
        })
      .to_return(:status => 200, :body => '{"hello": "world"}', :headers => {})

    class << request
      attr_accessor :path
    end

    request.path = VERIFY_URL.request_uri
    request
  end

end

我动态添加属性的最后一部分是解决另一个错误。感觉有点像脏黑客。也许有更好的方法来做这一切,这也会阻止我这样做?

在我的 spec_helper.rb 中,我添加了一行:

WebMock.disable_net_connect!(allow_localhost: true)

我已将请求的内容放在request方法中,一切似乎都没问题,我也使用了Net::HTTPRequest的两倍而不是webmock存根,但是我得到了同样的错误。我不想存根客户端因为那是我的测试系统。我还查看了Ruby文档中request方法的代码。我不明白......错误来自何处以及如何解决?

1 个答案:

答案 0 :(得分:0)

您不需要像现在一样使用stub_request的返回值。您可以正常构建Net::HTTP::Request的实例。