Thursday 14 October 2010

testing Iridium gateway and active merchant with Rspec

Recently I rewrote the billing section of a website. The site uses Iridium as the gateway.
I thought people might be interested in seeing how I spec'd this:


context "iridium" do
before do
BillingDetail.gateway =
ActiveMerchant::Billing::IridiumGateway.new(
:login => "Casdasdasd",
:password => "asdasdasd",
:enable_3d_secure => true)
end

it "should work" do
bd = Factory.build(:unsaved_billing_detail)

bd.should_receive(:ready!)

credit_card = Factory.build(:iridium_good_no_3ds)
address = Factory.build(:iridium_good_no_3ds_address)
bd.address = address

bd.authorize(100, credit_card)
end

it "should fail" do
bd = Factory.build(:unsaved_billing_detail)

bd.should_receive(:fail!)

credit_card = Factory.build(:iridium_card_declined)
address = Factory.build(:iridium_card_declined_address)
bd.address = address

lambda {
bd.authorize(100, credit_card)
}.should raise_error(TGR::GatewayError)
end
end



I use FactoryGirl, so I set up some factories:


Factory.define :iridium_good_no_3ds, :class => ActiveMerchant::Billing::CreditCard do |cc|
cc.number "4976000000003436"
cc.last_name "Watson"
cc.first_name "John"
cc.verification_value "452"
cc.month "12"
cc.year "2012"
end

Factory.define :iridium_good_no_3ds_address, :class => Address do |address|
address.last_name "Watson"
address.first_name "John"
address.address_1 "32 Edward Street"
address.city "Camborne"
address.state "Cornwall"
address.zipcode "TR14 8PA"
address.country "GB"
end


Factory.define :iridium_card_declined, :class => ActiveMerchant::Billing::CreditCard do |cc|
cc.number "4921810000009076"
cc.last_name "Lewis"
cc.first_name "Jack"
cc.verification_value "875"
cc.month "12"
cc.year "2012"
end

Factory.define :iridium_card_declined_address, :class => Address do |address|
address.last_name "Lewis"
address.first_name "Jack"
address.address_1 "4 Wing Road"
address.city "Leighton Buzzard"
address.state "Bedfordshire"
address.zipcode "LU7 0JB"
address.country "GB"
end


This seems to work a treat. I can do all my other tests using the bogus gateway, but to make sure I am actually providing the right info for Iridium this context, with its before block to set the right gateway, does the job nicely!

No comments: