Skip to content

Instantly share code, notes, and snippets.

@nightcrawler-
Created January 16, 2022 10:01
Show Gist options
  • Save nightcrawler-/3765846ad910a36c38353fbc8c09bba2 to your computer and use it in GitHub Desktop.
Save nightcrawler-/3765846ad910a36c38353fbc8c09bba2 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
# == Schema Information
#
# Table name: contributions
#
# id :bigint not null, primary key
# aasm_state :string
# amount :integer default(0)
# error_message :string
# message :text
# mpesa_fees :decimal(, ) default(0.0)
# phone_number :string
# platform_fees :decimal(, ) default(0.0)
# source :string
# created_at :datetime not null
# updated_at :datetime not null
# campaign_id :bigint
# contributor_id :bigint
#
require 'rails_helper'
RSpec.describe Contribution, type: :model do
fixtures :campaigns, :users
subject do
described_class.create!(campaign: campaigns(:one), contributor_id: users(:lizzy).id, source: 0, amount: 1000, phone_number: '+254723006561')
end
describe 'Validations' do
it { expect(subject).to validate_numericality_of(:amount).is_greater_than_or_equal_to(1) }
it { expect(subject).to validate_presence_of(:amount) }
it { expect(subject).to validate_presence_of(:source) }
end
describe 'Associations' do
it { expect(subject).to belong_to(:contributor) }
it { expect(subject).to belong_to(:campaign) }
it { expect(subject).to have_one(:payment_online_payment_request) }
it { expect(subject).to have_one(:payment_paybill_confirmation) }
end
describe 'Callbacks' do
it { expect(subject).to callback(:process_contribution).after(:create) }
it { expect(subject).to callback(:normalize_number).before(:validation) }
end
describe 'Initial state' do
it { is_expected.to have_state(:pending) }
end
describe 'Apply fees' do
it 'should apply current fees regime' do
subject.process
subject.complete # use state machine to apply charges
expect(subject.mpesa_fees).to eq 12
end
end
describe 'AASM State' do
it { expect(subject).to transition_from(:pending).to(:processing).on_event(:process) }
it { expect(subject).to transition_from(:processing).to(:completed).on_event(:complete) }
it { expect(subject).to transition_from(:processing).to(:failed).on_event(:failure) }
end
# describe "Process contribution" do
# it "calls relevant methods" do
# subject.process
# expect(subject).to receive(:log_status_change)
# end
# end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment