使用Webmock伪造成功的ActiveMerchant响应

时间:2012-11-27 06:41:10

标签: ruby-on-rails rspec activemerchant webmock

我正在使用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需要的东西,以便注册响应实际上是成功的?

1 个答案:

答案 0 :(得分: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>