From bfeb63c8d7f84799ba64dd2dfb60cd0f67907c96 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 11 Jun 2015 14:16:18 +1000 Subject: [PATCH] Ensure that changes to Spree::Config.products_require_tax_category do not leak out of the relevant spec --- spec/features/admin/products_spec.rb | 43 +++++++++++++++------------- spec/models/spree/product_spec.rb | 4 +-- spec/spec_helper.rb | 1 + spec/support/products_helper.rb | 12 ++++++++ 4 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 spec/support/products_helper.rb diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index a82674ddaf..57b3aa0dfe 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -117,31 +117,34 @@ feature %q{ end end - scenario "creating a new product", js: true do - Spree::Config.products_require_tax_category = false - click_link 'Products' - click_link 'New Product' + context "products do not require a tax category" do + around { |example| with_products_require_tax_category(false) { example.run } } - fill_in 'product_name', :with => 'A new product !!!' - fill_in 'product_price', :with => '19.99' + scenario "creating a new product", js: true do + click_link 'Products' + click_link 'New Product' - page.should have_selector('#product_supplier_id') - select 'Another Supplier', :from => 'product_supplier_id' - select 'Weight (g)', from: 'product_variant_unit_with_scale' - fill_in 'product_unit_value_with_description', with: '500' - select taxon.name, from: "product_primary_taxon_id" - select 'None', from: "product_tax_category_id" + fill_in 'product_name', :with => 'A new product !!!' + fill_in 'product_price', :with => '19.99' - # Should only have suppliers listed which the user can manage - page.should have_select 'product_supplier_id', with_options: [@supplier2.name, @supplier_permitted.name] - page.should_not have_select 'product_supplier_id', with_options: [@supplier.name] + page.should have_selector('#product_supplier_id') + select 'Another Supplier', :from => 'product_supplier_id' + select 'Weight (g)', from: 'product_variant_unit_with_scale' + fill_in 'product_unit_value_with_description', with: '500' + select taxon.name, from: "product_primary_taxon_id" + select 'None', from: "product_tax_category_id" - click_button 'Create' + # Should only have suppliers listed which the user can manage + page.should have_select 'product_supplier_id', with_options: [@supplier2.name, @supplier_permitted.name] + page.should_not have_select 'product_supplier_id', with_options: [@supplier.name] - flash_message.should == 'Product "A new product !!!" has been successfully created!' - product = Spree::Product.find_by_name('A new product !!!') - product.supplier.should == @supplier2 - product.tax_category.should be_nil + click_button 'Create' + + flash_message.should == 'Product "A new product !!!" has been successfully created!' + product = Spree::Product.find_by_name('A new product !!!') + product.supplier.should == @supplier2 + product.tax_category.should be_nil + end end scenario "editing a product" do diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 505ac8ee2e..dfaf715b35 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -40,7 +40,7 @@ module Spree describe "tax category" do context "when a tax category is required" do - before { Spree::Config.products_require_tax_category = true } + around { |example| with_products_require_tax_category(true) { example.run } } it "is invalid when a tax category is not provided" do build(:product, tax_category_id: nil).should_not be_valid @@ -48,7 +48,7 @@ module Spree end context "when a tax category is not required" do - before { Spree::Config.products_require_tax_category = false } + around { |example| with_products_require_tax_category(false) { example.run } } it "is valid when a tax category is not provided" do build(:product, tax_category_id: nil).should be_valid diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a911fe7742..2b3903dece 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -91,6 +91,7 @@ RSpec.configure do |config| config.include OpenFoodNetwork::ControllerHelper, :type => :controller config.include OpenFoodNetwork::FeatureToggleHelper config.include OpenFoodNetwork::EnterpriseGroupsHelper + config.include OpenFoodNetwork::ProductsHelper config.include OpenFoodNetwork::DistributionHelper config.include OpenFoodNetwork::HtmlHelper config.include ActionView::Helpers::DateHelper diff --git a/spec/support/products_helper.rb b/spec/support/products_helper.rb new file mode 100644 index 0000000000..8e9ebc1c98 --- /dev/null +++ b/spec/support/products_helper.rb @@ -0,0 +1,12 @@ +module OpenFoodNetwork + module ProductsHelper + def with_products_require_tax_category(value) + original_value = Spree::Config.products_require_tax_category + + Spree::Config.products_require_tax_category = value + yield + ensure + Spree::Config.products_require_tax_category = original_value + end + end +end