I feel i’m missing something wrt to setting up factory girl so that I can use methods like ‘build’ and ‘create’ without having to call FactoryGirl.build or FactoryGirl.create. As I get started using factory_girl I just want to have a good foundation before creating some bad habits. Here is a minimal example that illustrates what I’m hitting. Any tips appreciated.
rspec -fd spec/some_spec.rb
stuff
has stuff (FAILED - 1)
Failures:
1) stuff has stuff
Failure/Error: let(:resource) { create :stuff }
NoMethodError:
undefined method `create' for #<RSpec::ExampleGroups::Stuff:0x007f9c3a102650>
# ./spec/some_spec.rb:3:in `block (2 levels) in <top (required)>'
# ./spec/some_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.00042 seconds (files took 0.23289 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/some_spec.rb:4 # stuff has stuff
tree spec
spec
├── factories.rb
├── some_spec.rb
├── spec_helper.rb
└── support
└── factory_girl.rb
1 directory, 4 files
head -1 spec/spec_helper.rb
require 'factory_girl'
cat spec/support/factory_girl.rb
# spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
cat spec/factories.rb
FactoryGirl.define do
factory :stuff, class: String do
skip_create
initialize_with { "This is stuff" }
end
end
cat spec/some_spec.rb
require_relative 'spec_helper'
describe 'stuff' do
let(:resource) { create :stuff }
it 'has stuff' do
expect(resource).to eq("Blah")
end
end