Last active
February 8, 2022 02:46
-
-
Save serradura/9a57a51a9995503bc58ec405b3042bb0 to your computer and use it in GitHub Desktop.
Using dependency injection to improve testability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'rspec', '~> 3.10' | |
gem 'activesupport', require: 'active_support/all' | |
end | |
module Calc | |
def self.get_tax(amount, time: Time.now) | |
tax = 7 | |
tax = tax + 10 if amount.to_f >= 1000 | |
if time.on_weekday? | |
tax = 5 if time.hour >= 9 && time.hour <= 18 | |
end | |
tax | |
end | |
end | |
require 'rspec/autorun' | |
RSpec.describe Calc do | |
describe '.get_tax' do | |
context 'when the amount is greater than or equal to 1000' do | |
context 'and now is a weekday but is not a business hour' do | |
it 'returns 17' do | |
time = double(on_weekday?: true, hour: 8) | |
tax = Calc.get_tax(1001, time: time) | |
expect(tax).to be == 17 | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Refactoring
Calc.get_tax
.