From 3db8a120ba617d57c5bda4fb5a31907661581b38 Mon Sep 17 00:00:00 2001 From: Michael Koper Date: Sun, 5 Aug 2018 17:12:23 -0400 Subject: [PATCH] Do not raise error for valid build_stubbed methods decrement, increment, and toggle do not persist the toggled values, so there is not reason to disable those methods. Co-authored-by: Alex Tsui --- lib/factory_bot/strategy/stub.rb | 3 --- spec/acceptance/build_stubbed_spec.rb | 29 +++++++++++++++++++++++--- spec/factory_bot/strategy/stub_spec.rb | 3 --- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/factory_bot/strategy/stub.rb b/lib/factory_bot/strategy/stub.rb index efc776c..8c52daf 100644 --- a/lib/factory_bot/strategy/stub.rb +++ b/lib/factory_bot/strategy/stub.rb @@ -6,17 +6,14 @@ module FactoryBot DISABLED_PERSISTENCE_METHODS = [ :connection, :decrement!, - :decrement, :delete, :destroy!, :destroy, :increment!, - :increment, :reload, :save!, :save, :toggle!, - :toggle, :touch, :update!, :update, diff --git a/spec/acceptance/build_stubbed_spec.rb b/spec/acceptance/build_stubbed_spec.rb index 67847b4..2a32ba2 100644 --- a/spec/acceptance/build_stubbed_spec.rb +++ b/spec/acceptance/build_stubbed_spec.rb @@ -7,7 +7,8 @@ describe "a generated stub instance" do define_model('Post', title: :string, body: :string, age: :integer, - user_id: :integer) do + user_id: :integer, + draft: :boolean) do belongs_to :user end @@ -77,13 +78,35 @@ describe "a generated stub instance" do expect { subject.save }.to raise_error(RuntimeError) end - it "disables increment" do + it "disables increment!" do expect { subject.increment!(:age) }.to raise_error(RuntimeError) end - it "disables decrement" do + it "disables decrement!" do expect { subject.decrement!(:age) }.to raise_error(RuntimeError) end + + it "disables toggle!" do + expect { subject.toggle!(:draft) }.to raise_error(RuntimeError) + end + + it "allows increment" do + subject.age = 1 + subject.increment(:age) + expect(subject.age).to eq(2) + end + + it "allows decrement" do + subject.age = 1 + subject.decrement(:age) + expect(subject.age).to eq(0) + end + + it "allows toggle" do + subject.draft = true + subject.toggle(:draft) + expect(subject).not_to be_draft + end end describe "calling `build_stubbed` with a block" do diff --git a/spec/factory_bot/strategy/stub_spec.rb b/spec/factory_bot/strategy/stub_spec.rb index f6b8abf..8fa04a1 100644 --- a/spec/factory_bot/strategy/stub_spec.rb +++ b/spec/factory_bot/strategy/stub_spec.rb @@ -48,17 +48,14 @@ describe FactoryBot::Strategy::Stub do end include_examples "disabled persistence method", :connection - include_examples "disabled persistence method", :decrement include_examples "disabled persistence method", :decrement! include_examples "disabled persistence method", :delete include_examples "disabled persistence method", :destroy include_examples "disabled persistence method", :destroy! - include_examples "disabled persistence method", :increment include_examples "disabled persistence method", :increment! include_examples "disabled persistence method", :reload include_examples "disabled persistence method", :save include_examples "disabled persistence method", :save! - include_examples "disabled persistence method", :toggle include_examples "disabled persistence method", :toggle! include_examples "disabled persistence method", :touch include_examples "disabled persistence method", :update