Add specs for Account#update_balance

This commit is contained in:
Pau Perez
2018-01-18 09:44:48 +01:00
parent fe87533032
commit b495ef0614

View File

@ -39,4 +39,68 @@ describe Account do
expect(organization.account.organization).to eq organization
end
describe '#update_balance' do
let(:account) { member.account }
context 'when the balance did not change since last balance update' do
before do
account.movements << Movement.new(amount: 5)
account.save
end
it 'updates the account balance' do
# a callback in Movement already called #update_balance after creating
# the movement above
account.update_balance
expect(account.balance).to eq(5)
end
it 'does not flag the account' do
# a callback in Movement already called #update_balance after creating
# the movement above
account.update_balance
expect(account.flagged).to be_falsy
end
end
context 'when the balance changed since last balance update' do
context 'and the new balance falls within the limits allowed' do
before do
account.max_allowed_balance = 100
account.min_allowed_balance = 0
account.movements << Movement.new(amount: 5)
account.save
end
it 'updates the account balance' do
# a callback in Movement calls #update_balance after creating the
# movement above
expect(account.balance).to eq(5)
end
it 'does not flag the account' do
# a callback in Movement calls #update_balance after creating the
# movement above
expect(account.flagged).to be_falsy
end
end
context 'and the new balance does not fall within the limits allowed' do
before do
account.max_allowed_balance = 0
account.min_allowed_balance = 0
account.movements << Movement.new(amount: 5)
account.save
end
it 'does not flag the account' do
# a callback in Movement calls #update_balance after creating the
# movement above
expect(account.flagged).to be_truthy
end
end
end
end
end