1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

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 <alextsui05@gmail.com>
This commit is contained in:
Michael Koper 2018-08-05 17:12:23 -04:00 committed by Daniel Colson
parent 5191056e1c
commit 3db8a120ba
3 changed files with 26 additions and 9 deletions

View file

@ -6,17 +6,14 @@ module FactoryBot
DISABLED_PERSISTENCE_METHODS = [ DISABLED_PERSISTENCE_METHODS = [
:connection, :connection,
:decrement!, :decrement!,
:decrement,
:delete, :delete,
:destroy!, :destroy!,
:destroy, :destroy,
:increment!, :increment!,
:increment,
:reload, :reload,
:save!, :save!,
:save, :save,
:toggle!, :toggle!,
:toggle,
:touch, :touch,
:update!, :update!,
:update, :update,

View file

@ -7,7 +7,8 @@ describe "a generated stub instance" do
define_model('Post', title: :string, define_model('Post', title: :string,
body: :string, body: :string,
age: :integer, age: :integer,
user_id: :integer) do user_id: :integer,
draft: :boolean) do
belongs_to :user belongs_to :user
end end
@ -77,13 +78,35 @@ describe "a generated stub instance" do
expect { subject.save }.to raise_error(RuntimeError) expect { subject.save }.to raise_error(RuntimeError)
end end
it "disables increment" do it "disables increment!" do
expect { subject.increment!(:age) }.to raise_error(RuntimeError) expect { subject.increment!(:age) }.to raise_error(RuntimeError)
end end
it "disables decrement" do it "disables decrement!" do
expect { subject.decrement!(:age) }.to raise_error(RuntimeError) expect { subject.decrement!(:age) }.to raise_error(RuntimeError)
end 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 end
describe "calling `build_stubbed` with a block" do describe "calling `build_stubbed` with a block" do

View file

@ -48,17 +48,14 @@ describe FactoryBot::Strategy::Stub do
end end
include_examples "disabled persistence method", :connection include_examples "disabled persistence method", :connection
include_examples "disabled persistence method", :decrement
include_examples "disabled persistence method", :decrement! include_examples "disabled persistence method", :decrement!
include_examples "disabled persistence method", :delete include_examples "disabled persistence method", :delete
include_examples "disabled persistence method", :destroy include_examples "disabled persistence method", :destroy
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", :increment!
include_examples "disabled persistence method", :reload include_examples "disabled persistence method", :reload
include_examples "disabled persistence method", :save include_examples "disabled persistence method", :save
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", :toggle!
include_examples "disabled persistence method", :touch include_examples "disabled persistence method", :touch
include_examples "disabled persistence method", :update include_examples "disabled persistence method", :update