Skip to content

Commit

Permalink
create rspec tests for recurring donations
Browse files Browse the repository at this point in the history
  • Loading branch information
winsonwan committed Mar 14, 2024
1 parent 3d4bed0 commit 99178bb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
43 changes: 41 additions & 2 deletions spec/controllers/store_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

describe StoreController do
before :each do ; @buyer = create(:customer) ; end

shared_examples_for 'initial visit' do
before :each do
@r = {:controller => 'store', :action => 'index'}
Expand Down Expand Up @@ -101,6 +101,45 @@
end
end

describe 'making a recurring donation' do
before :each do
@customer = create(:customer)
end
let (:attempt_recurring_donation) {
post :process_donation,
{:customer_id => @customer.id, :donation => 5, :donation_frequency => 'monthly', :credit_card_token => 'dummy'}
}
context 'when transaction completes successfully' do
before :each do
allow(Stripe::Charge).to receive(:create).and_return(double("Stripe::Charge", id: 1))
end
it 'creates a new RecurringDonation record' do
expect{attempt_recurring_donation}.to change {RecurringDonation.count}.by(1)
end
it 'sets new RecurringDonation record attributes to correct values' do
attempt_recurring_donation
recurring_donation = RecurringDonation.find(1)
donation = recurring_donation.donations[0]
expect(recurring_donation.account_code_id).to(equal(donation.account_code_id))
expect(recurring_donation.customer_id).to(equal(donation.customer_id))
end
it 'adds a recurring_donation_id foreign key to the first donation instance' do
attempt_recurring_donation
recurring_donation = RecurringDonation.find(1)
donation = recurring_donation.donations[0]
expect(donation.recurring_donation_id).to equal(recurring_donation.id)
end
end
context 'when transaction completes unsuccessfully' do
before :each do
allow(Stripe::Charge).to receive(:create).and_raise(Stripe::StripeError)
end
it 'does not create a new RecurringDonation record' do
expect{attempt_recurring_donation}.to change {RecurringDonation.count}.by(0)
end
end
end

describe 'quick donation with nonexistent customer' do
before :each do
@new_valid_customer = attributes_for(:customer).except(:password,:password_confirmation)
Expand Down Expand Up @@ -147,7 +186,7 @@
expect(flash[:alert]).to match(/Incomplete or invalid donor/i)
end
end

end
describe "processing empty cart" do
before :each do
Expand Down
11 changes: 10 additions & 1 deletion spec/models/order_adding_items_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rails_helper'

describe Order, 'adding' do
before :each do
before :each do
@order = Order.create!(:processed_by => create(:customer)) # generic customer
@vv = create(:valid_voucher)
end
Expand Down Expand Up @@ -47,6 +47,15 @@
expect(reloaded.donation.account_code_id).to eq(@donation.account_code_id)
end
end
describe 'recurring donations' do
before :each do
@order.add_donation(build(:donation, :amount => 17))
end
it 'should add recurring donation' do
@order.add_recurring_donation()
expect(@order.recurring_donation).to be_a_kind_of(RecurringDonation)
end
end
describe 'and getting total price' do
it 'without donation' do
vv2 = create(:valid_voucher)
Expand Down
10 changes: 10 additions & 0 deletions spec/models/order_finalizing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ def vouchers_for(showdate, vouchertype)
@order.finalize!
expect(@order.purchaser.donations).to include(@donation)
end
it 'should add recurring donation to customer account if purchaser==recipient' do
@order.add_recurring_donation()
@order.finalize!
expect(@order.purchaser.recurring_donations).to include(@order.recurring_donation)
end
it 'should associate donation with recurring donation' do
@order.add_recurring_donation()
@order.finalize!
expect(@donation.recurring_donation_id).to equal(@order.recurring_donation.id)
end
context 'when purchaser!=recipient' do
before :each do
@order.purchaser = create(:customer)
Expand Down

0 comments on commit 99178bb

Please sign in to comment.