2013-11-25 66 views
0

我對模型有3個測試,它們之間的主要區別是每個創建時傳遞的參數數量。我在每個版本的模型上運行相同的期望來啓動,所以有很多重複。我只是想知道是否有一個更乾淨的方式來做到這一點。這裏是我的代碼:RSPEC - 創建具有不同參數的模型/避免重複的方法

describe Item do 
    subject {Item.new(:report_id => 15 , :name => 'Gas' , :tax_id => 1 , :category_id => 15 , :sub_category_id => 1 , :job_id => 1 , :total => 20)} 
    let(:tax) {Tax.where(id: subject.tax_id).first} 
    let(:sub_category) {SubCategory.where(id: subject.sub_category_id).first} 


    it 'Calculate with just Total' do 
    subject.name.should be == 'Gas'  
    subject.set_nil_values 
    sub_category.should_receive(:taxable).and_return(sub_category.taxable) 
    tax.should_receive(:rate).and_return(tax.rate) 
    sub_category.should_receive(:tax_adjustment).and_return(sub_category.tax_adjustment) 
    subject.calculate_tax 
    subject.calculate_cost 
    subject.cost.should be_within(0.01).of(17.70) 
    subject.tax_amount.should be_within(0.01).of(2.30) 
    subject.save 
    end 

end 

    describe Item do 
    subject {Item.new(:report_id => 15 , :name => 'Gas' , :tax_id => 1 , :category_id => 15 , :sub_category_id => 1 , :job_id => 1 , :tax_override => 2.30 , :total => 20 )} 
    let(:sub_category) {SubCategory.where(id: subject.sub_category_id).first} 


    it 'Calculate with tax override' do 
     @tax = Tax.find(subject.tax_id) 
     subject.name.should be == 'Gas' 
     tax = Tax.find_by_id(subject.tax_id) 

     subject.set_nil_values 
     sub_category.should_receive(:taxable).exactly(1).times.and_return(sub_category.taxable) 
     tax.should_receive(:rate).exactly(1).times.and_return(tax.rate) 
     sub_category.should_receive(:tax_adjustment).exactly(1).times.and_return(sub_category.tax_adjustment) 
     subject.calculate_tax 
     subject.calculate_cost 
     subject.cost.should be_within(0.01).of(17.70) 
     subject.tax_amount.should be_within(0.01).of(2.30) 
     subject.save 
    end 

    describe Item do 
     subject {Item.new( :report_id => 16 , :name => 'Gas' , :tax_id => 1 , :category_id => 15 , :sub_category_id => 1 , :job_id => 1 , :tax_override => 1.15 , :unclaimed_tax => 1.15 , :total => 20 
     )} 
     let(:sub_category) {SubCategory.where(id: subject.sub_category_id).first} 


     it 'Calculate with Unclaimed Tax' do 
      @tax = Tax.find(subject.tax_id) 
      subject.name.should be == 'Gas' 
      tax = Tax.find_by_id(subject.tax_id) 

      subject.set_nil_values 
      sub_category.should_receive(:taxable).exactly(1).times.and_return(sub_category.taxable) 
      tax.should_receive(:rate).exactly(1).times.and_return(tax.rate) 
      sub_category.should_receive(:tax_adjustment).exactly(1).times.and_return(sub_category.tax_adjustment) 
      subject.calculate_tax 
      subject.calculate_cost 
      subject.cost.should be_within(0.01).of(17.70) 
      subject.tax_amount.should be_within(0.01).of(1.15) 
      subject.unclaimed_tax.should be_within(0.01).of(1.15) 
      subject.save 
     end 
    end 

回答

0

你應該使用工廠和FactoryGirl寶石。 另外,請適當地格式化您的代碼,這很難閱讀。

相關問題