thoughtbot / thoughtbot/factory_bot
Allow factories to use RSpec example methods such as `double`
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 8.2k
- Forks
- 2.6k
- PR merge metrics
- No merged PRs in 30d
Description
This can either be an improvement or something to add to the documentation as suggested by Daniel J Colson on Twitter.
The simple feature is to, at least, use RSpec's double within a factory's initialize_with.
How
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
FactoryBot::Evaluator.include RSpec::Mocks::ExampleMethods
end
end
Why?
I've written a test suite for a third party API and I need to mock the response. For external requests I'm using Webmock with VCR but that has no bearing on this particular issue. The issue is to test my own code before any external API ever needs to be hit. Here's a sample:
FactoryBot.define do
Response = Trav3::Response
factory :response do
initialize_with do
dbl = double
allow(dbl).to receive(:body).and_return( { a: :b, c: :d }.to_json )
new(build(:travis), dbl)
end
end
end
Now I can use the nice and readable format of double within initialize_with and don't have to resort to:
dbl = Object.new
dbl.define_singleton_method(:body) { { a: :b, c: :d }.to_json }
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the initialize_with example using FactoryBot::Evaluator and RSpec::Mocks::ExampleMethods, then inspect how FactoryBot evaluates initialize_with blocks. Compare the requested double and allow calls with the documented configuration workaround. Done means the example works through the supported factory setup, with coverage or documentation reflecting the chosen behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- testing
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100