From f95c6a2386d7da463d745e7f873a2a517cc10a93 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 5 Apr 2013 14:11:31 +1100 Subject: [PATCH] Extract stub building into private method --- .../split_products_by_distribution_spec.rb | 63 +++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/spec/lib/open_food_web/split_products_by_distribution_spec.rb b/spec/lib/open_food_web/split_products_by_distribution_spec.rb index eceb1d6620..afba1ebb9d 100644 --- a/spec/lib/open_food_web/split_products_by_distribution_spec.rb +++ b/spec/lib/open_food_web/split_products_by_distribution_spec.rb @@ -17,11 +17,9 @@ describe OpenFoodWeb::SplitProductsByDistribution do it "splits products by distributor when a distributor is selected" do distributor = double(:distributor) - local_product, remote_product = double(:product), double(:product) - local_product.should_receive(:in_distributor?).any_number_of_times. - with(distributor).and_return(true) - remote_product.should_receive(:in_distributor?).any_number_of_times. - with(distributor).and_return(false) + + local_product = build_product distributor, true, nil, false + remote_product = build_product distributor, false, nil, false products, products_local, products_remote = subject.split_products_by_distribution [local_product, remote_product], distributor, nil @@ -32,11 +30,9 @@ describe OpenFoodWeb::SplitProductsByDistribution do it "splits products by order cycle when an order cycle is selected" do order_cycle = double(:order_cycle) - local_product, remote_product = double(:product), double(:product) - local_product.should_receive(:in_order_cycle?).any_number_of_times. - with(order_cycle).and_return(true) - remote_product.should_receive(:in_order_cycle?).any_number_of_times. - with(order_cycle).and_return(false) + + local_product = build_product nil, false, order_cycle, true + remote_product = build_product nil, false, order_cycle, false products, products_local, products_remote = subject.split_products_by_distribution [local_product, remote_product], nil, order_cycle @@ -49,28 +45,10 @@ describe OpenFoodWeb::SplitProductsByDistribution do distributor = double(:distributor) order_cycle = double(:order_cycle) - neither_product, distributor_product, order_cycle_product, both_product = - double(:product), double(:product), double(:product), double(:product) - - neither_product.should_receive(:in_distributor?).any_number_of_times. - with(distributor).and_return(false) - neither_product.should_receive(:in_order_cycle?).any_number_of_times. - with(order_cycle).and_return(false) - - distributor_product.should_receive(:in_distributor?).any_number_of_times. - with(distributor).and_return(true) - distributor_product.should_receive(:in_order_cycle?).any_number_of_times. - with(order_cycle).and_return(false) - - order_cycle_product.should_receive(:in_distributor?).any_number_of_times. - with(distributor).and_return(false) - order_cycle_product.should_receive(:in_order_cycle?).any_number_of_times. - with(order_cycle).and_return(true) - - both_product.should_receive(:in_distributor?).any_number_of_times. - with(distributor).and_return(true) - both_product.should_receive(:in_order_cycle?).any_number_of_times. - with(order_cycle).and_return(true) + neither_product = build_product distributor, false, order_cycle, false + distributor_product = build_product distributor, true, order_cycle, false + order_cycle_product = build_product distributor, false, order_cycle, true + both_product = build_product distributor, true, order_cycle, true orig_products = [neither_product, distributor_product, order_cycle_product, both_product] @@ -80,4 +58,25 @@ describe OpenFoodWeb::SplitProductsByDistribution do products_local.should == [both_product] products_remote.should == [neither_product, distributor_product, order_cycle_product] end + + + + private + + def build_product(distributor, in_distributor, order_cycle, in_order_cycle) + product = double(:product) + + if distributor + product.should_receive(:in_distributor?).any_number_of_times. + with(distributor).and_return(in_distributor) + end + + if order_cycle + product.should_receive(:in_order_cycle?).any_number_of_times. + with(order_cycle).and_return(in_order_cycle) + end + + product + end + end