Our documentation has moved!

You are currently viewing a legacy version of our help articles.
For the most up-to-date version, please use the new Chargify Help & Support Site.
Follow

Automated site testing from Ruby on Rails

Ryan Schlesinger (ryan@instanceinc.com) is one of our merchants and he had this to say about automated testing of his Chargify setup:

I use FakeWeb extensively for this purpose. Here are some examples:

def make_transactions_response

payment = [{:product_id => 4707,
    :ending_balance_in_cents => 0,
    :memo => "Yeah",
    :id => Sham.unique_id,
    :amount_in_cents => 2000,
    :type => "Payment",
    :success => true }]
charge = [{:product_id => 4707,
    :ending_balance_in_cents => 2000,
    :memo => "Basic Monthly",
    :id => Sham.unique_id,
    :amount_in_cents => 2000,
    :type => "Charge",
    :success => true }]

resp = payment + charge

FakeWeb.register_uri(:get, %r|#{test_domain}/subscriptions/\d+/transactions.xml$|, :status => 200, :body => resp.to_xml(:root => "transactions"))
FakeWeb.register_uri(:get, %r|#{test_domain}/subscriptions/\d+/transactions.xml\?kinds.*payment|, :status => 200, :body => payment.to_xml(:root => "transactions"))
FakeWeb.register_uri(:get, %r|#{test_domain}/subscriptions/\d+/transactions.xml\?kinds.*charge|, :status => 200, :body => charge.to_xml(:root => "transactions"))

end

You can also register multiple responses for the same URL to simulate needing to run a reload on your model to pick up changes. I use this to test creating a subscription and then waiting for the payment on that subscription.

resp is a successful subscription creation
resp2 is the same as resp but with the balance set to 0

FakeWeb.register_uri(:get, %r|#{test_domain}/subscriptions/\d+.xml|,
[{:status => 200, :body => resp.to_xml, :times => 2}, {:status => 200, :body => resp2.to_xml}])

See the FakeWeb site for more info: http://fakeweb.rubyforge.org/

Also I prefer using FakeWeb.allow_net_connect = false to alert me when I've missed a URL.##

I stole test_domain from the chargify gem. Here's the definition:

def test_domain

"#{Chargify::Base.connection.site.scheme}://#{Chargify::Base.connection.user}:#{Chargify::Base.connection.password}@#{Chargify::Base.connection.site.host}:#{Chargify::Base.connection.site.port}"

end

Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk