【问题标题】:Using Webmock to fake successful ActiveMerchant response使用 Webmock 伪造成功的 ActiveMerchant 响应
【发布时间】:2012-11-14 17:58:10
【问题描述】:

我正在使用 ActiveMerchant 与 Authorize.net CIM 集成。我正在编写自动化测试,我已经开始实施 Webmock 调用,这样我的测试就不会在每次运行时都访问 Authorize.net。

我已经根据原始请求数据的响应创建了 XML 文件,并且在大多数情况下,它运行良好。但是,当我模拟一个成功的响应时,ActiveMerchant 出于某种原因仍然告诉我 Response.success?不是真的。

我的功能

if self.cim_customer_profile_id.nil?
  ActiveMerchant::Billing::Base.mode = :test

  customer_profile_information = {
    :profile     => {
      :merchant_customer_id => self.customer.username.first(20),
      :email => self.customer.email
    }
  }

  gateway = ActiveMerchant::Billing::AuthorizeNetCimGateway.new(
    :login    => AUTHORIZE_NET_API_LOGIN_ID,
    :password => AUTHORIZE_NET_API_TRANSACTION_KEY
  )

  response = gateway.create_customer_profile(customer_profile_information)

  if response.success?
    self.cim_customer_profile_id = response.authorization
  else
    raise StandardError, response.message
  end
end

然后我的回复是:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8

<?xml version="1.0" encoding="utf-8"?>
<createCustomerProfileResponse xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='AnetApi/xml/v1/schema/AnetApiSchema.xsd'>
  <messages>
    <resultCode>
      Ok
    </resultCode>
    <message>
      <code>
        I00001
      </code>
      <text>
        Successful.
      </text>
    </message>
  </messages>
  <customerProfileId>10793616</customerProfileId>
  <customerPaymentProfileIdList/>
  <customerShippingAddressIdList/>
  <validationDirectResponseList/>
</createCustomerProfileResponse>

有什么原因导致 ActiveMerchant 无法处理成功的存根请求?或者我是否遗漏了 ActiveMerchant 需要的东西才能注册响应实际上是成功的?

【问题讨论】:

    标签: ruby-on-rails rspec activemerchant webmock


    【解决方案1】:

    啊,我太笨了。为了便于阅读,我在所有 XML 标记之后添加了新行,但它们干扰了 ActiveMerchant 解析和评估响应的方式。

    所以正确的 XML 响应模拟应该是:

    HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    <?xml version="1.0" encoding="utf-8"?>
    <createCustomerProfileResponse xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='AnetApi/xml/v1/schema/AnetApiSchema.xsd'>
      <messages>
        <resultCode>Ok</resultCode>
        <message>
          <code>I00001</code>
          <text>Successful.</text>
        </message>
      </messages>
      <customerProfileId>10793616</customerProfileId>
      <customerPaymentProfileIdList/>
      <customerShippingAddressIdList/>
      <validationDirectResponseList/>
    </createCustomerProfileResponse>
    

    【讨论】:

      猜你喜欢
      • 2015-07-13
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      • 2013-06-22
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多