FactoryAllStub
Factory All Stub is a spell to force FactoryBot use only build_stubbed strategy (even if you call create or build).
The idea behind it is to quickly fix Factory Doctor offenses (and even do that automatically).
NOTE. Only works with FactoryBot. Should be considered only as a temporary specs fix.
Instructions
First, you have to initialize FactoryAllStub:
TestProf::FactoryAllStub.initThe initialization process injects custom logic into FactoryBot generator.
To enable all-stub mode:
TestProf::FactoryAllStub.enable!To disable all-stub mode and use factories as always:
TestProf::FactoryAllStub.disable!RSpec
In your spec_helper.rb (or rails_helper.rb if any):
require "test_prof/recipes/rspec/factory_all_stub"That would automatically initialize FactoryAllStub (no need to call .init) and provide "factory:stub" shared context with enables it for the marked examples or example groups:
describe "User" do
let(:user) { create(:user) }
it "is valid", factory: :stub do
# use `build_stubbed` instead of `create`
expect(user).to be_valid
end
endFactoryAllStub was designed to be used with FactoryDoctor the following way:
# Run FactoryDoctor and mark all offensive examples with factory:stub
FDOC=1 FDOC_STAMP=factory:stub rspec ./spec/models