From cc409b52533a84bad1b02a6703cda8c08c1f4ec7 Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Mon, 19 Jun 2017 23:41:33 -0400 Subject: [PATCH] issue #112 moved ingredients delete call to an else block, build a custom matcher for collections, rewrote test that involved removing a recipe and not losing particular ingredients in the current grocery list --- app/models/grocery_list.rb | 3 ++- spec/models/meal_plan_spec.rb | 6 +++++- spec/support/custom_matchers.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 spec/support/custom_matchers.rb diff --git a/app/models/grocery_list.rb b/app/models/grocery_list.rb index 481fca5..b16d41c 100644 --- a/app/models/grocery_list.rb +++ b/app/models/grocery_list.rb @@ -27,9 +27,10 @@ def subtract_ingredient(sub_i) if list_i.matched?(sub_i) if list_i.sub(sub_i) return true + else + return self.ingredients.delete(list_i) end end - return self.ingredients.delete(list_i) end end diff --git a/spec/models/meal_plan_spec.rb b/spec/models/meal_plan_spec.rb index 281b04e..0156f32 100644 --- a/spec/models/meal_plan_spec.rb +++ b/spec/models/meal_plan_spec.rb @@ -101,7 +101,11 @@ mealplan1.recipes << recipe7 mealplan1.recipes << recipe9 mealplan1.recipes.delete(recipe7) - expect(mealplan1.user.grocery_list.ingredients).to eq recipe7.ingredients.first + expect(mealplan1.user.grocery_list.ingredients).to contain_one_or_more_records_that(have_attributes( + :unit => recipe7.ingredients.first.unit, + :amount => recipe7.ingredients.first.amount, + :name => recipe7.ingredients.first.name) + ) end end diff --git a/spec/support/custom_matchers.rb b/spec/support/custom_matchers.rb new file mode 100644 index 0000000..0481ae8 --- /dev/null +++ b/spec/support/custom_matchers.rb @@ -0,0 +1,8 @@ +# Helps to loop through a collection in search of one particular item that matches. +RSpec::Matchers.define :contain_one_or_more_records_that do |matcher| + match do |collection| + collection.any? do |item| + matcher.matches?(item) + end + end +end