Stub strategy disables all persistence instance methods
Previously, instances created with the Stub strategy disabled a number of
methods defined by ActiveRecord::Persistence, to ensure that stubbed instances
did not read or write to the database. However, this blacklist was incomplete,
allowing some methods (e.g. #save!) to be called on stub instances.
This expands the blacklist to disable all methods defined by
ActiveRecord::Persistence that are not explicitly defined on stub instances
(currently #new_record?, #persisted?), or do not talk to the database at all
(#becomes, #becomes!).
The blacklist is correct as of current Rails master (the pre-release for Rails
5.0). It also, as a result, includes a couple of methods that are not defined
by ActiveRecord::Persistence in earlier versions of Rails, and will not
automatically include methods added to this module in future versions of
Rails.
It would be possible to make this automatic using reflection, and instead
using a whitelist of allowed methods:
ActiveRecord::Persistence.public_instance_methods
However, at present this test is not dependent on ActiveRecord, so I have left
this for future work if desired.
2016-06-29 13:36:29 -04:00
|
|
|
shared_examples "disabled persistence method" do |method_name|
|
|
|
|
let(:instance) { described_class.new.result(evaluation) }
|
|
|
|
|
|
|
|
describe "overriding persistence method: ##{method_name}" do
|
|
|
|
it "overrides the method with any arity" do
|
|
|
|
method = instance.method(method_name)
|
|
|
|
|
|
|
|
expect(method.arity).to eq(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an informative error if the method is called" do
|
|
|
|
expect { instance.send(method_name) }.to raise_error(
|
|
|
|
RuntimeError,
|
|
|
|
"stubbed models are not allowed to access the database - #{instance.class}##{method_name}()",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Strategy::Stub do
|
2012-05-04 13:36:46 -04:00
|
|
|
it_should_behave_like "strategy with association support", :build_stubbed
|
2012-02-08 10:17:57 -05:00
|
|
|
it_should_behave_like "strategy with callbacks", :after_stub
|
2012-05-04 13:36:46 -04:00
|
|
|
it_should_behave_like "strategy with strategy: :build", :build_stubbed
|
2011-08-12 22:06:10 -04:00
|
|
|
|
2011-08-16 23:08:24 -04:00
|
|
|
context "asking for a result" do
|
2011-09-02 16:22:53 -04:00
|
|
|
before { Timecop.freeze(Time.now) }
|
2011-12-16 14:10:25 -05:00
|
|
|
let(:result_instance) do
|
|
|
|
define_class("ResultInstance") do
|
2018-05-01 17:17:26 -04:00
|
|
|
attr_accessor :id, :created_at
|
2011-12-16 14:10:25 -05:00
|
|
|
end.new
|
|
|
|
end
|
|
|
|
|
2017-06-01 12:54:02 -04:00
|
|
|
let(:evaluation) do
|
|
|
|
double("evaluation", object: result_instance, notify: true)
|
|
|
|
end
|
2011-09-02 16:22:53 -04:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
it { expect(subject.result(evaluation)).not_to be_new_record }
|
|
|
|
it { expect(subject.result(evaluation)).to be_persisted }
|
2017-01-15 08:26:03 -05:00
|
|
|
it { expect(subject.result(evaluation)).not_to be_destroyed }
|
2011-08-12 22:06:10 -04:00
|
|
|
|
2011-09-02 16:22:53 -04:00
|
|
|
it "assigns created_at" do
|
2012-04-13 14:20:19 -04:00
|
|
|
created_at = subject.result(evaluation).created_at
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(created_at).to eq Time.now
|
2011-09-02 16:22:53 -04:00
|
|
|
|
|
|
|
Timecop.travel(150000)
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.result(evaluation).created_at).to eq created_at
|
2011-09-02 16:22:53 -04:00
|
|
|
end
|
|
|
|
|
Stub strategy disables all persistence instance methods
Previously, instances created with the Stub strategy disabled a number of
methods defined by ActiveRecord::Persistence, to ensure that stubbed instances
did not read or write to the database. However, this blacklist was incomplete,
allowing some methods (e.g. #save!) to be called on stub instances.
This expands the blacklist to disable all methods defined by
ActiveRecord::Persistence that are not explicitly defined on stub instances
(currently #new_record?, #persisted?), or do not talk to the database at all
(#becomes, #becomes!).
The blacklist is correct as of current Rails master (the pre-release for Rails
5.0). It also, as a result, includes a couple of methods that are not defined
by ActiveRecord::Persistence in earlier versions of Rails, and will not
automatically include methods added to this module in future versions of
Rails.
It would be possible to make this automatic using reflection, and instead
using a whitelist of allowed methods:
ActiveRecord::Persistence.public_instance_methods
However, at present this test is not dependent on ActiveRecord, so I have left
this for future work if desired.
2016-06-29 13:36:29 -04:00
|
|
|
include_examples "disabled persistence method", :connection
|
|
|
|
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", :reload
|
|
|
|
include_examples "disabled persistence method", :save
|
|
|
|
include_examples "disabled persistence method", :save!
|
|
|
|
include_examples "disabled persistence method", :toggle!
|
|
|
|
include_examples "disabled persistence method", :touch
|
|
|
|
include_examples "disabled persistence method", :update
|
|
|
|
include_examples "disabled persistence method", :update!
|
|
|
|
include_examples "disabled persistence method", :update_attribute
|
|
|
|
include_examples "disabled persistence method", :update_attributes
|
|
|
|
include_examples "disabled persistence method", :update_attributes!
|
|
|
|
include_examples "disabled persistence method", :update_column
|
|
|
|
include_examples "disabled persistence method", :update_columns
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
end
|