Skip to content

Instantly share code, notes, and snippets.

@dnagir
Created June 28, 2012 23:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnagir/3014807 to your computer and use it in GitHub Desktop.
Save dnagir/3014807 to your computer and use it in GitHub Desktop.
Fast specs with Rails
ENV["RAILS_ENV"] ||= 'test'
cur_dir = File.expand_path(File.dirname(__FILE__) + '/..')
$LOAD_PATH << "#{cur_dir}"
if defined? Rails
# Most likely going with the full env
puts "Running faster_helper with full Rails env..."
# Eagerly load the Rails so that stubbed dependencies still work
# http://stackoverflow.com/questions/11133712/using-rails-model-that-is-already-declared
Rails.application.eager_load!
require 'spec_helper'
else
$LOAD_PATH << "#{cur_dir}/app/models"
$LOAD_PATH << "#{cur_dir}/lib"
require 'spec/support/rspec'
end
# example of a spec
require 'faster_helper'
require 'investment_report/tax'
module InvestmentReport
describe Tax, "#on_amount" do
def self.tax_on(amount, &block)
context "tax on #{amount}" do
subject { Tax.on_amount(amount) }
it(&block)
end
end
def self.rate_on(amount, &block)
context "tax rate on #{amount}" do
subject { Tax.rate_on(amount) }
it(&block)
end
end
before { Tax.stub(medicare_levy_on: medicare_levy) }
let(:medicare_levy) { 100 }
context "<=6k: nil" do
rate_on(0) { should == 0 }
tax_on(0) { should == 0 }
rate_on(3000) { should == 0 }
tax_on(3000) { should == 0 }
rate_on(6000) { should == 0 }
tax_on(6000) { should == 0 }
end
context "6-37k: 15% over 6k plus medicare levy" do
let(:medicare_levy) { 123456 }
rate_on(6001) { should == 0.15 }
tax_on(6000.01) { should be_within(0.01).of 0.01*0.15 + medicare_levy }
tax_on(10_000) { should be_within(0.01).of 4000*0.15 + medicare_levy }
tax_on(37_000) { should be_within(0.01).of 31_000*0.15 + medicare_levy }
rate_on(37_000) { should == 0.15 }
end
context "37-80k: $4650 plus 30% over $37000 plus medicare levy" do
rate_on(37_001) { should == 0.30 }
tax_on(37_000.01) { should be_within(0.01).of 4650 + 0.01*0.3 + medicare_levy }
tax_on(80_000) { should be_within(0.01).of 4650 + 43_000*0.30 + medicare_levy }
rate_on(80_000) { should == 0.30 }
end
context "80-180k: $17,550 plus 37% over $80,000 plus medicare levy" do
rate_on(80_001) { should == 0.37 }
tax_on(80_000.01) { should be_within(0.01).of 17_550 + 0.01*0.37 + medicare_levy }
tax_on(100_000) { should be_within(0.01).of 17_550 + 20_000*0.37 + medicare_levy }
tax_on(180_000) { should be_within(0.01).of 17_550 + 100_000*0.37 + medicare_levy }
rate_on(180_000) { should == 0.37 }
end
context "180+: $54,550 plus 45% over $180,000 plus medicare levy" do
rate_on(180_001) { should == 0.45 }
tax_on(180_000.01) { should be_within(0.01).of 54_550 + 0.01*0.45 + medicare_levy }
tax_on(500_000) { should be_within(0.01).of 54_550 + 320_000*0.45 + medicare_levy }
rate_on(500_000) { should == 0.45 }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment