在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
的双倍而不是网络货物存根,但是我遇到了同样的错误。我不想让客户固执,因为那是我正在测试的系统。我还查看了Ruby文档中request
方法的代码。我不明白...错误来自哪里以及如何解决?
您不需要像以前那样使用stub_request
的返回值。您可以正常构建Net::HTTP::Request
的实例。