我正在尝试在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
此Using webmock to mock XMLRPC client in rspec-rails对我也不起作用。
非常感谢任何帮助。 谢谢!
答案 0 :(得分:2)
使用此代码创建响应。
<textarea>
before do
stub_request(:post, "http://api-test.com/host_add").to_return(
{ :body => XMLRPC::Create.new.methodResponse(true, { hoge: "test"} ), :status => 200 }
)
end
是对xml的哈希
答案 1 :(得分:0)
该代码对我有用:
context "success" do
before do
stub_request(:post, "http://srv476.offsidebet.com:8080/abp/webApiXmlRpcServlet").
with(:body => "<?xml version=\"1.0\" ?><methodCall><methodName>func</methodName><params><param><value><string>a</string></value></param><param><value><string>b</string></value></param><param><value><string>c</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'=>'238', 'Content-Type'=>'text/xml; charset=utf-8', 'User-Agent'=>'XMLRPC::Client (Ruby 2.1.1)'}).
to_return(:status => 200, :body => File.read("#{Rails.root}/spec/fixtures/xml_rpc_call_helper/response_ok.xml"), :headers => {})
end
let(:response){ {"wm_description"=>"manual create", "wm_ticket_number"=>"1408171142773", "ABPBuildNumber"=>"436 r${svn.Revision}", "wm_payment_number"=>"41902112", "wm_sim_mode"=>"0", "wm_redirect_url"=>"https://exmaple.com/entrypoint", "WEBAPIVersionNumber"=>"0.57", "wm_purse"=>"12345"} }
it {XmlRpcCallHelper.xmlrpc_call(XML_RPC_LOCATION, true, 'func', 'a','b','c').should eq(response)}
end#success
夹具:
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>wm_description</name>
<value>manual create</value>
</member>
<member>
<name>wm_ticket_number</name>
<value>1408171142773</value>
</member>
<member>
<name>ABPBuildNumber</name>
<value>436 r${svn.Revision}</value>
</member>
<member>
<name>wm_payment_number</name>
<value>41902112</value>
</member>
<member>
<name>wm_sim_mode</name>
<value>0</value>
</member>
<member>
<name>wm_redirect_url</name>
<value>https://exmaple.com/entrypoint</value>
</member>
<member>
<name>WEBAPIVersionNumber</name>
<value>0.57</value>
</member>
<member>
<name>wm_purse</name>
<value>12345</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>