Skip to content

Instantly share code, notes, and snippets.

@taylor
Created February 16, 2009 12:24
Show Gist options
  • Save taylor/65143 to your computer and use it in GitHub Desktop.
Save taylor/65143 to your computer and use it in GitHub Desktop.
# So I bought this PDF book on ActiveMerchant from Peepcode.
# It's tests use Test::Unit, I was using RSpec. So I translated them.
# http://peepcode.com/products/activemerchant-pdf
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
include ActiveMerchant::Billing
describe OrderTransaction do
before do
@amount = 100
end
describe "authorizing" do
describe "with successful authorization" do
before do
@auth = OrderTransaction.authorize(@amount, credit_card(:number => '1'))
end
it "succeeds" do
@auth.should be_success
end
it "returns a success message" do
@auth.message.should == BogusGateway::SUCCESS_MESSAGE
end
it "wants to authorize" do
@auth.action.should == 'authorization'
end
it "has an authorization reference" do
@auth[:reference].should == BogusGateway::AUTHORIZATION
end
end
describe "with failed authorization" do
before do
@auth = OrderTransaction.authorize(@amount, credit_card(:number => '2'))
end
it "doesn't succeed" do
@auth.should_not be_success
end
it "returns a failure message" do
@auth.message.should == BogusGateway::FAILURE_MESSAGE
end
it "wants to authorize" do
@auth.action.should == 'authorization'
end
end
describe "with exception" do
before do
@auth = OrderTransaction.authorize(@amount, credit_card(:number => '3'))
end
it "doesn't succeed" do
@auth.should_not be_success
end
it "returns an error message" do
@auth.message.should == BogusGateway::ERROR_MESSAGE
end
it "wants to authorize" do
@auth.action.should == 'authorization'
end
end
end
describe "capturing" do
describe "with success" do
before do
@capt = OrderTransaction.capture(@amount, '123')
end
it "succeeds" do
@capt.should be_success
end
it "want's to capture" do
@capt.action.should == 'capture'
end
it "returns a success message" do
@capt.message.should == BogusGateway::SUCCESS_MESSAGE
end
end
describe "with failure" do
before do
@capt = OrderTransaction.capture(@amount, '2')
end
it "doesn't succeed" do
@capt.should_not be_success
end
it "want's to capture" do
@capt.action.should == 'capture'
end
it "returns a failure message" do
@capt.message == BogusGateway::CAPTURE_ERROR_MESSAGE
end
end
describe "with error" do
before do
@auth = OrderTransaction.capture(@amount, '1')
end
it "doesn't succeed" do
@auth.should_not be_success
end
it "wants to capture" do
@auth.action.should == 'capture'
end
it "returns an error message" do
@auth.message.should == BogusGateway::CAPTURE_ERROR_MESSAGE
end
end
end
end
# ...
def credit_card_hash(options = {})
{ :number => '1',
:first_name => 'Cody',
:last_name => 'Fauser',
:month => '8',
:year => "#{ Time.now.year + 1 }",
:verification_value => '123',
:type => 'visa'
}.update(options)
end
def credit_card(options = {})
ActiveMerchant::Billing::CreditCard.new( credit_card_hash(options) )
end
def address(options = {})
{ :name => 'Cody Fauser',
:address1 => '2500 Oak Mills Road',
:address2 => 'Suite 1000',
:city => 'Beverly Hills',
:state => 'CA',
:country => 'US',
:zip => '90210'
}.update(options)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment