Net::HTTP 请求中的参数数量错误
wrong number of arguments in Net::HTTP request
在 Sinatra 应用程序中,我试图围绕 Net::HTTP
制作我自己的包装器,但我被困在我的一个测试中。我正在使用 Webmock 存根请求并将其传递给 Net::HTTP
的 request
方法。我使用 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
方法的代码。我不明白...错误来自何处以及如何解决?
您不需要像现在这样使用 stub_request
的 return 值。您可以像往常一样构造 Net::HTTP::Request
的实例。
在 Sinatra 应用程序中,我试图围绕 Net::HTTP
制作我自己的包装器,但我被困在我的一个测试中。我正在使用 Webmock 存根请求并将其传递给 Net::HTTP
的 request
方法。我使用 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
方法的代码。我不明白...错误来自何处以及如何解决?
您不需要像现在这样使用 stub_request
的 return 值。您可以像往常一样构造 Net::HTTP::Request
的实例。