我对模型进行了3次测试,它们之间的主要区别在于每次创建时传递的参数数量。我几乎在模型的每个版本上运行相同的期望,所以有很多重复。我只是想知道是否有更清洁的方法来做到这一点。这是我的代码:
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