Rspec: How to I refactor tests that are similar? (example given) -
i have 2 examples, feel majority of code inside of them same. however, bit different (the records different, , additional assertion in 2nd 1 too). i'm still beginner @ testing, looking tips go forward. i'm testing rake task. here's code:
it 'leaves 1 billing info each order' order = factorygirl.create(:order) factorygirl.create_list(:billing_info, 2, order_id: order.id) expect(billinginfo.all.count).to eq(2) run_rake_task expect(billinginfo.all.count).to eq(1) end 'keeps billing info trevance information' order = factorygirl.create(:order) factorygirl.create(:billing_info, order_id: order.id, complete_trevance_message: nil, trevance_attempts: nil) factorygirl.create(:billing_info, order_id: order.id, complete_trevance_message: "303 -- processor decline", trevance_attempts: 1) expect(billinginfo.all.count).to eq(2) run_rake_task expect(billinginfo.all.count).to eq(1) expect(billinginfo.first.complete_trevance_message).to eq("303 -- processor decline") end
as can see, similar. okay split these 2 this? or there better way?
in opinion dry isn't best rule in tests. means have hide code in method harder read etc. woudn't change much, there things can done in simpler way.
context "billing rake task" let(:order) { factorygirl.create(:order) } 'leaves 1 billing info each order' factorygirl.create_list(:billing_info, 2, order_id: order.id) expect { run_rake_task }.to change { billinginfo.count }.by(-1) end 'keeps billing info trevance information' factorygirl.create(:billing_info, order_id: order.id, complete_trevance_message: nil, trevance_attempts: nil) factorygirl.create(:billing_info, order_id: order.id, complete_trevance_message: "303 -- processor decline", trevance_attempts: 1) expect { run_rake_task }.to change { billinginfo.count }.by(-1) expect(billinginfo.first.complete_trevance_message).to eq("303 -- processor decline") end end
notice changes spec little bit, don't check if count changed 2 one, changed 1 less. think here better, can't sure don't know application well.
Comments
Post a Comment