Skip to content
Browse files

Make test flow clearer and more stylish

  • Loading branch information...
1 parent 4ec4ead commit 51d71b83e3b1f2f80df74084fbb27b066a56af4b @gabebw gabebw committed
View
52 spec/fake_braintree/credit_card_spec.rb
@@ -2,16 +2,18 @@
describe 'Braintree::CreditCard.find' do
it 'gets the correct credit card' do
- credit_card = Braintree::CreditCard.find(token)
+ month = '04'
+ year = '2016'
+ credit_card = Braintree::CreditCard.find(token_for(month, year))
credit_card.last_4.should == TEST_CC_NUMBER[-4,4]
credit_card.expiration_month.should == month
credit_card.expiration_year.should == year
end
- let(:month) { '04' }
- let(:year) { '2016' }
- let(:token) { braintree_credit_card_token(TEST_CC_NUMBER, [month, year].join('/')) }
+ def token_for(month, year)
+ braintree_credit_card_token(TEST_CC_NUMBER, [month, year].join('/'))
+ end
end
describe 'Braintree::CreditCard.sale' do
@@ -24,22 +26,6 @@
describe 'Braintree::CreditCard.create' do
- def build_credit_card_hash
- {
- customer_id: @customer && @customer.id,
- number: '4111111111111111',
- cvv: '123',
- token: 'token',
- expiration_date: '07/2020',
- billing_address: {
- postal_code: '94110'
- },
- options: {
- make_default: true
- }
- }
- end
-
it 'allows creating a credit card without a customer' do
result = Braintree::CreditCard.create(build_credit_card_hash)
result.should be_success
@@ -55,7 +41,7 @@ def build_credit_card_hash
FakeBraintree.decline_all_cards!
result = Braintree::CreditCard.create(build_credit_card_hash)
result.should_not be_success
- expect { Braintree::CreditCard.find('token') }.to raise_exception Braintree::NotFoundError
+ expect { Braintree::CreditCard.find('token') }.to raise_error Braintree::NotFoundError
FakeBraintree.clear!
end
@@ -63,7 +49,7 @@ def build_credit_card_hash
FakeBraintree.verify_all_cards!
result = Braintree::CreditCard.create(build_credit_card_hash.merge(number: '12345'))
result.should_not be_success
- expect { Braintree::CreditCard.find('token') }.to raise_exception Braintree::NotFoundError
+ expect { Braintree::CreditCard.find('token') }.to raise_error Braintree::NotFoundError
FakeBraintree.verify_all_cards = false
end
@@ -71,7 +57,7 @@ def build_credit_card_hash
result = Braintree::CreditCard.create(build_credit_card_hash)
result.should be_success
Braintree::Customer.find(@customer.id).credit_cards.last.token.should == 'token'
- Braintree::Customer.find(@customer.id).credit_cards.last.default?.should be_true
+ Braintree::Customer.find(@customer.id).credit_cards.last.should be_default
Braintree::Customer.find(@customer.id).credit_cards.last.billing_address.postal_code.should == "94110"
end
@@ -82,10 +68,26 @@ def build_credit_card_hash
result.should be_success
# Reload the customer
@customer = Braintree::Customer.find(@customer.id)
- @customer.credit_cards.select {|c| c.default?}.length.should == 1
+ @customer.credit_cards.select(&:default?).length.should == 1
@customer.credit_cards.length.should == 2
end
end
+
+ def build_credit_card_hash
+ {
+ customer_id: @customer && @customer.id,
+ number: '4111111111111111',
+ cvv: '123',
+ token: 'token',
+ expiration_date: '07/2020',
+ billing_address: {
+ postal_code: '94110'
+ },
+ options: {
+ make_default: true
+ }
+ }
+ end
end
describe 'Braintree::CreditCard.update' do
@@ -99,6 +101,6 @@ def build_credit_card_hash
end
it 'raises an error for a nonexistent credit card' do
- lambda { Braintree::CreditCard.update('foo', {number: TEST_CC_NUMBER}) }.should raise_error(Braintree::NotFoundError)
+ expect { Braintree::CreditCard.update('foo', number: TEST_CC_NUMBER) }.to raise_error(Braintree::NotFoundError)
end
end
View
8 spec/fake_braintree/customer_spec.rb
@@ -137,7 +137,7 @@
end
it 'raises an error for a nonexistent customer' do
- lambda { Braintree::Customer.update('foo', {first_name: 'Bob'}) }.should raise_error(Braintree::NotFoundError)
+ expect { Braintree::Customer.update('foo', {first_name: 'Bob'}) }.to raise_error(Braintree::NotFoundError)
end
it 'does not allow a customer to be updated to a failing credit card' do
@@ -146,11 +146,7 @@
customer = create_customer
result = Braintree::Customer.update(customer.customer.id,
- {
- credit_card: {
- number: bad_credit_card
- }
- }
+ credit_card: { number: bad_credit_card }
)
result.should_not be_success
end
View
16 spec/fake_braintree/subscription_spec.rb
@@ -45,8 +45,10 @@
it 'can find a created subscription' do
payment_method_token = cc_token
plan_id = 'abc123'
- subscription_id =
- create_subscription(payment_method_token: payment_method_token, plan_id: plan_id).subscription.id
+ subscription_id = create_subscription(
+ payment_method_token: payment_method_token,
+ plan_id: plan_id
+ ).subscription.id
subscription = Braintree::Subscription.find(subscription_id)
subscription.should_not be_nil
subscription.payment_method_token.should == payment_method_token
@@ -79,16 +81,17 @@
describe 'Braintree::Subscription.update' do
it 'can update a subscription' do
+ subscription_id = create_subscription.subscription.id
+
Braintree::Subscription.update(subscription_id, plan_id: 'a_new_plan')
Braintree::Subscription.find(subscription_id).plan_id.should == 'a_new_plan'
end
-
- let(:subscription_id) { subscription.subscription.id }
- let(:subscription) { create_subscription }
end
describe 'Braintree::Subscription.cancel' do
it 'can cancel a subscription' do
+ subscription_id = create_subscription.subscription.id
+
Braintree::Subscription.cancel(subscription_id).should be_success
Braintree::Subscription.find(subscription_id).status.should == Braintree::Subscription::Status::Canceled
end
@@ -96,7 +99,4 @@
it 'cannot cancel an unknown subscription' do
expect { Braintree::Subscription.cancel('totally-bogus-id') }.to raise_error(Braintree::NotFoundError)
end
-
- let(:subscription_id) { subscription.subscription.id }
- let(:subscription) { create_subscription }
end
View
13 spec/fake_braintree/transaction_spec.rb
@@ -97,9 +97,9 @@
describe FakeBraintree::SinatraApp do
context 'Braintree::Transaction.find' do
it 'can find a created sale' do
- id = create_transaction.id
+ id = create_transaction(10.00).id
result = Braintree::Transaction.find(id)
- result.amount.should == amount
+ result.amount.should == 10.00
end
it 'can find >1 transaction' do
@@ -111,10 +111,11 @@
expect { Braintree::Transaction.find('foobar') }.to raise_error(Braintree::NotFoundError)
end
- def create_transaction
- Braintree::Transaction.sale(payment_method_token: cc_token, amount: amount).transaction
+ def create_transaction(amount = 10.00)
+ Braintree::Transaction.sale(
+ payment_method_token: cc_token,
+ amount: amount
+ ).transaction
end
-
- let(:amount) { 10.00 }
end
end
View
27 spec/fake_braintree_spec.rb
@@ -67,18 +67,17 @@ def write_to_log
describe FakeBraintree, 'VALID_CREDIT_CARDS' do
it 'includes only credit cards that are valid in the Braintree sandbox' do
- FakeBraintree::VALID_CREDIT_CARDS.sort.should == valid_credit_cards.sort
- end
+ valid_credit_cards = %w(
+ 4111111111111111 4005519200000004
+ 4009348888881881 4012000033330026
+ 4012000077777777 4012888888881881
+ 4217651111111119 4500600000000061
+ 5555555555554444 378282246310005
+ 371449635398431 6011111111111117
+ 3530111333300000
+ )
- let(:valid_credit_cards) do
- %w(4111111111111111 4005519200000004
- 4009348888881881 4012000033330026
- 4012000077777777 4012888888881881
- 4217651111111119 4500600000000061
- 5555555555554444 378282246310005
- 371449635398431 6011111111111117
- 3530111333300000
- )
+ FakeBraintree::VALID_CREDIT_CARDS.sort.should == valid_credit_cards.sort
end
end
@@ -123,8 +122,10 @@ def write_to_log
it 'has a timestamp of Time.now' do
Timecop.freeze do
- transaction = FakeBraintree.generate_transaction(status: Braintree::Transaction::Status::Failed,
- subscription_id: 'my_subscription_id')
+ transaction = FakeBraintree.generate_transaction(
+ status: Braintree::Transaction::Status::Failed,
+ subscription_id: 'my_subscription_id'
+ )
transaction['status_history'].first['timestamp'].should == Time.now
end
end
View
8 spec/support/subscription_helpers.rb
@@ -1,8 +1,10 @@
module SubscriptionHelpers
- def create_subscription(options = {})
- Braintree::Subscription.create({
+ def create_subscription(user_options = {})
+ options = {
payment_method_token: cc_token,
plan_id: 'my_plan_id'
- }.merge(options))
+ }.merge(user_options)
+
+ Braintree::Subscription.create(options)
end
end

0 comments on commit 51d71b8

Please sign in to comment.
Something went wrong with that request. Please try again.