0

我正在尝试在 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对我也不起作用。

任何帮助深表感谢。谢谢!

4

2 回答 2

2

使用此代码创建响应。

before do
  stub_request(:post, "http://api-test.com/host_add").to_return(
    { :body => XMLRPC::Create.new.methodResponse(true, { hoge: "test"} ), :status => 200 }
  )
end

XMLRPC::Create.new.methodResponse是散列到 xml

于 2015-05-06T12:17:30.997 回答
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>
于 2014-08-23T01:39:17.480 回答