我正在尝试在 Rails 中的 rspec 测试中存根 XMLRPC::Client.call。
这是我的方法:
def xmlrpc_call(location, repeat, func, *args)
Rails.logger.debug("XML_RPC: calling #{func}(#{args.inspect})")
timeout = XMLRPC_TIMEOUT ? XMLRPC_TIMEOUT : 90
begin
server = XMLRPC::Client.new2(location, nil, timeout)
result = server.call(func, *args)
rescue XMLRPC::FaultException => e
Rails.logger.error "XMLRPC FaultException: #{e.faultCode}, Exception: #{e.faultString}"
log_abp_error("#{func}: #{e.faultCode} (wrapper, rescue 1, try: #{(repeat ? "1":"2")})")
@xmlrpc_exception = e
false
end
result
end#xmlrpc_call
这是我的测试:
describe "#xmlrpc_call" do
it 'should return success' do
stub_request(:post, "http://google.com/RPC2").
with(:body => "<?xml version=\"1.0\" ?><methodCall> <methodName>CallGoogle</methodName><params><param><value><string>foo</string></value></param><param><value><string>bar</string></value></param></params></methodCall>\n",
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Connection'=>'keep-alive', 'Content-Length'=>'200', 'Content-Type'=>'text/xml; charset=utf-8', 'User-Agent'=>'XMLRPC::Client (Ruby 2.1.1)'}).
to_return(:status => 200, :body => "success", :headers => {})
XmlRpcCallHelper.xmlrpc_call("http://google.com", true, "CallGoogle", "foo", "bar")
end#success
end#xmlrpc_call
我收到此错误:
Failure/Error: XmlRpcCallHelper.xmlrpc_call("http://google.com", true, "CallGoogle", "foo", "bar")
RuntimeError:
Missing return value!
如果我在存根中使用空响应体,我会得到:
Failure/Error: XmlRpcCallHelper.xmlrpc_call("http://google.com", true, "CallGoogle", "foo", "bar")
NoMethodError:
undefined method `bytesize' for nil:NilClass
This Using webmock to mock XMLRPC client in rspec-rails对我也不起作用。
任何帮助深表感谢。谢谢!