Hi Michael

Some thoughts:
* I'd be putting your comments as the descriptions for the `it` statements - 
perhaps comments in tests are a code smell?
* It might be better comparing against the actual values (do the math 
elsewhere).
* If you've only got one example per context, then is there much point?

describe TaxCalculator do
  describe '#calculate' do
    it "is zero for incomes under 6000" do
      subject.income = 5_000
      subject.calculate.should == 0
    end
  
    it "adds 15c for amounts over 6000" do
      subject.income = 10_000
      subject.calculate.should == 600
    end
  
    it "adds 30c for amounts over 37000" do
      subject.income = 50_000
      subject.calculate.should == 8_550
    end
  end
end

Or as an alternative, could try many more values with a bit of metaprogramming, 
covering edge cases.

describe TaxCalculator do
  describe '#calculate' do
    {
      0      =>      0.00,
      6_000  =>      0.00,
      6_001  =>      0.15,
      37_000 =>  4_650.00,
      37_001 =>  4_650.30,
      80_000 => 17_550.00
    }.each do |income, tax|
      it "calculates #{tax} for an income of #{income}" do
        subject.income = income
        subject.calculate.should == tax
      end
    end
  end
end

I'd probably opt for the first, though - being a little more descriptive is not 
a bad thing.

-- 
Pat

On 02/09/2011, at 12:34 PM, Michael Gall wrote:

> Hi guys,
> 
> I'd love to get some feedback on a spec I'm writing. It's a tax calculator 
> for australian taxation rates - nothing too complex just yet, but some of the 
> calulation specs feel wrong, but I don't know a better way.
> 
> Thanks in advance.
> 
> 
> describe TaxCalculator do
>   describe "low income bracket" {
>     before { subject.income = 5000 }
>     it { subject.calculate.should == 0 }
>   }
>   
>   describe "15c tax bracket" {
>     before { subject.income = 10000 }
>     it { subject.calculate.should == 4000 * 0.15 } # the tax bracket is 15c 
> in the dollar between 6000 and 37000
>   }
>   
>   describe "30c tax bracket" {
>     before { subject.income = 50000 }
>     it { subject.calculate.should == 6000 * 0 + 31000 * 0.15 + 13000 * 0.30 } 
> # the tax bracket is 30c in the dollar between 37000 and 80000
>   }
> end
> 
> 
> Cheers,
> 
> 
> Michael
> 
> 
> 
> 
> -- 
> Checkout my new website: http://myachinghead.net
> http://wakeless.net
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rails-oceania?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to