Permalink
Browse files

Merge pull request #78 from bmcdaniel11/master

Updated FactoryGirl to FactoryBot
  • Loading branch information...
mathieujobin committed Nov 17, 2017
2 parents 7bef802 + 3eb80f9 commit 41933f349c253c270754863bf44874c9351d3d9c
@@ -8,7 +8,7 @@ GIT
PATH
remote: .
specs:
pickle (0.5.3)
pickle (0.5.4)
cucumber (>= 0.8, < 3.0)
rake

@@ -86,7 +86,7 @@ GEM
diff-lcs (1.3)
docile (1.1.5)
erubis (2.7.0)
factory_girl (4.8.0)
factory_bot (4.8.2)
activesupport (>= 3.0.0)
gherkin (4.1.3)
git (1.3.0)
@@ -187,7 +187,7 @@ DEPENDENCIES
cucumber-rails
database_cleaner
fabrication!
factory_girl
factory_bot
git
machinist
pickle!
@@ -198,4 +198,4 @@ DEPENDENCIES
yard

BUNDLED WITH
1.15.3
1.16.0
@@ -1,3 +1,6 @@
== 0.5.4
* updated FactoryGirl to FactoryBot

== 0.5.3
* small fix to pickle_path_for_resources_action_segment
* update for fabrication adapter with protected method klass
@@ -88,7 +88,7 @@ Given a post exists with title: "My Post", body: "My body"

### Using with factory-girl or machinist

But you're using Machinist or FactoryGirl right?! To leverage all of the factories/blueprints you've written, you can just do stuff like
But you're using Machinist or FactoryBot right?! To leverage all of the factories/blueprints you've written, you can just do stuff like

```gherkin
Given a user exists
@@ -107,14 +107,14 @@ In your `features/support/env.rb` add the following lines at the bottom
require "#{Rails.root}/spec/blueprints" # or wherever they live
```

#### FactoryGirl: make sure factories are loaded
#### FactoryBot: make sure factories are loaded

In your config/environments/cucumber.rb file, make sure the factory-girl gem is included (unless it's installed as a plugin).

If that doesn't solve loading issues then require your factories.rb file directly in a file called 'features/support/factory_girl.rb'
If that doesn't solve loading issues then require your factories.rb file directly in a file called 'features/support/factory_bot.rb'

```ruby
# example features/support/factory_girl.rb
# example features/support/factory_bot.rb
require File.dirname(__FILE__) + '/../../spec/factories'
```

@@ -174,7 +174,7 @@ Given a user exists with name: "Fred"
Given a user exists with name: "Fred", activated: false
```

This last step could be better expressed by using Machinist/FactoryGirl to create an activated user. Then you can do
This last step could be better expressed by using Machinist/FactoryBot to create an activated user. Then you can do

```gherkin
Given an activated user exists with name: "Fred"
@@ -1,13 +1,13 @@
# Factories
require 'factory_girl'
require 'factory_bot'

FactoryGirl.define do
FactoryBot.define do
sequence :fork_name do |n|
"fork %d04" % n
end

factory :fork do |f|
f.name { FactoryGirl.generate(:fork_name) }
f.name { FactoryBot.generate(:fork_name) }
end

factory :tine do |t|
@@ -20,6 +20,6 @@
end

factory :fancy_fork, :class => Fork do |t|
t.name { "Fancy " + FactoryGirl.generate(:fork_name) }
t.name { "Fancy " + FactoryBot.generate(:fork_name) }
end
end
@@ -7,9 +7,9 @@
# require 'machinist/active_record' # or your chosen adaptor
# require File.dirname(__FILE__) + '/../../spec/blueprints' # or wherever your blueprints are
#
# For FactoryGirl add: features/support/factory_girl.rb
# For FactoryBot add: features/support/factory_bot.rb
#
# require 'factory_girl'
# require 'factory_bot'
# require File.dirname(__FILE__) + '/../../spec/factories' # or wherever your factories are
#
# For Fabrication, just include it in the adapter list when configuring pickle as explained below.
@@ -98,11 +98,11 @@ def create(attrs = {})
end

# factory-girl adapter
class FactoryGirl < Adapter
class FactoryBot < Adapter
def self.factories
if defined? ::FactoryGirl
if defined? ::FactoryBot
factories = []
::FactoryGirl.factories.each do |factory|
::FactoryBot.factories.each do |factory|
factory.names.each do |name|
factories << new(factory, name)
end
@@ -114,24 +114,24 @@ def self.factories
end

def initialize(factory, factory_name)
if defined? ::FactoryGirl
if defined? ::FactoryBot
@klass, @name = factory.build_class, factory_name.to_s
else
@klass, @name = factory.build_class, factory.factory_name.to_s
end
end

def create(attrs = {})
if defined? ::FactoryGirl
::FactoryGirl.create(@name, attrs)
if defined? ::FactoryBot
::FactoryBot.create(@name, attrs)
else
Factory(@name, attrs)
end
end

def build(attrs = {})
if defined? ::FactoryGirl
::FactoryGirl.build(@name, attrs)
if defined? ::FactoryBot
::FactoryBot.build(@name, attrs)
else
Factory.build(@name, attrs)
end
@@ -11,7 +11,7 @@ def configure(&block)
end

def adapters
@adapters ||= [:machinist, :factory_girl, :fabrication, :orm]
@adapters ||= [:machinist, :factory_bot, :fabrication, :orm]
end

def adapter_classes
@@ -1,3 +1,3 @@
module Pickle
VERSION = "0.5.3"
VERSION = "0.5.4"
end
@@ -29,7 +29,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec-rails", "~>3.0"
s.add_development_dependency "rails", "~>4.2.6"
s.add_development_dependency "cucumber-rails"
s.add_development_dependency "factory_girl"
s.add_development_dependency "factory_bot"
s.add_development_dependency "fabrication", '~> 2.0'
s.add_development_dependency "machinist" # , "~>2.0"
s.add_development_dependency "database_cleaner"
@@ -7,9 +7,9 @@
# require 'machinist/active_record' # or your chosen adaptor
# require File.dirname(__FILE__) + '/../../spec/blueprints' # or wherever your blueprints are
#
# For FactoryGirl add: features/support/factory_girl.rb
# For FactoryBot add: features/support/factory_bot.rb
#
# require 'factory_girl'
# require 'factory_bot'
# require File.dirname(__FILE__) + '/../../spec/factories' # or wherever your factories are
#
# For Fabrication, just include it in the adapter list when configuring pickle as explained below.
@@ -1,7 +1,7 @@
require 'spec_helper'

require 'active_record'
require 'factory_girl'
require 'factory_bot'
require 'fabrication'
require 'machinist/active_record'
require 'pickle/adapters/active_record'
@@ -58,17 +58,17 @@ class Three < ActiveRecord::Base; end
end
end

describe 'FactoryGirl' do
describe 'FactoryBot' do
before do
allow(Pickle::Adapter::FactoryGirl).to receive(:model_classes).and_return([One, One::Two, Three])

if defined? ::FactoryGirl
@orig_factories = ::FactoryGirl.factories.dup
::FactoryGirl.factories.clear
::FactoryGirl::Syntax::Default::DSL.new.factory(:one, :class => One) {}
::FactoryGirl::Syntax::Default::DSL.new.factory(:two, :class => One::Two) {}
@factory1 = ::FactoryGirl.factories[:one]
@factory2 = ::FactoryGirl.factories[:two]
allow(Pickle::Adapter::FactoryBot).to receive(:model_classes).and_return([One, One::Two, Three])

if defined? ::FactoryBot
@orig_factories = ::FactoryBot.factories.dup
::FactoryBot.factories.clear
::FactoryBot::Syntax::Default::DSL.new.factory(:one, :class => One) {}
::FactoryBot::Syntax::Default::DSL.new.factory(:two, :class => One::Two) {}
@factory1 = ::FactoryBot.factories[:one]
@factory2 = ::FactoryBot.factories[:two]
else
@orig_factories, Factory.factories = Factory.factories, {}
Factory.define(:one, :class => One) {}
@@ -79,23 +79,23 @@ class Three < ActiveRecord::Base; end
end

after do
if defined? ::FactoryGirl
::FactoryGirl.factories.clear
@orig_factories.each {|f| ::FactoryGirl.factories.add(f) }
if defined? ::FactoryBot
::FactoryBot.factories.clear
@orig_factories.each {|f| ::FactoryBot.factories.add(f) }
else
Factory.factories = @orig_factories
end
end

it ".factories should create one for each factory" do
expect(Pickle::Adapter::FactoryGirl).to receive(:new).with(@factory1, @factory1.name).once
expect(Pickle::Adapter::FactoryGirl).to receive(:new).with(@factory2, @factory2.name).once
Pickle::Adapter::FactoryGirl.factories
expect(Pickle::Adapter::FactoryBot).to receive(:new).with(@factory1, @factory1.name).once
expect(Pickle::Adapter::FactoryBot).to receive(:new).with(@factory2, @factory2.name).once
Pickle::Adapter::FactoryBot.factories
end

describe ".new(factory, factory_name)" do
before do
@factory = Pickle::Adapter::FactoryGirl.new(@factory1, @factory1.name)
@factory = Pickle::Adapter::FactoryBot.new(@factory1, @factory1.name)
end

it "should have name of factory_name" do
@@ -106,7 +106,7 @@ class Three < ActiveRecord::Base; end
expect(@factory.klass).to eq(One)
end

unless defined? ::FactoryGirl
unless defined? ::FactoryBot
it "#create(attrs) should call Factory(<:key>, attrs)" do
expect(Factory).to receive(:create).with("one", {:key => "val"})
@factory.create(:key => "val")
@@ -5,12 +5,12 @@
@config = Pickle::Config.new
end

it "#adapters should default to :machinist, :factory_girl, :orm" do
expect(@config.adapters).to eq([:machinist, :factory_girl, :fabrication, :orm])
it "#adapters should default to :machinist, :factory_bot, :orm" do
expect(@config.adapters).to eq([:machinist, :factory_bot, :fabrication, :orm])
end

it "#adapter_classes should default to Adapter::Machinist, Adapter::FactoryGirl, Adapter::Orm" do
expect(@config.adapter_classes).to eq([Pickle::Adapter::Machinist, Pickle::Adapter::FactoryGirl, Pickle::Adapter::Fabrication, Pickle::Adapter::Orm])
it "#adapter_classes should default to Adapter::Machinist, Adapter::FactoryBot, Adapter::Orm" do
expect(@config.adapter_classes).to eq([Pickle::Adapter::Machinist, Pickle::Adapter::FactoryBot, Pickle::Adapter::Fabrication, Pickle::Adapter::Orm])
end

describe "setting adapters to [:machinist, SomeAdapter]" do
@@ -28,26 +28,26 @@ class SomeAdapter; end
describe "#factories" do
it "should call adaptor.factories for each adaptor" do
expect(Pickle::Adapter::Machinist).to receive(:factories).and_return([])
expect(Pickle::Adapter::FactoryGirl).to receive(:factories).and_return([])
expect(Pickle::Adapter::FactoryBot).to receive(:factories).and_return([])
expect(Pickle::Adapter::Fabrication).to receive(:factories).and_return([])
expect(Pickle::Adapter::Orm).to receive(:factories).and_return([])
@config.factories
end

it "should aggregate factories into a hash using factory name as key" do
expect(Pickle::Adapter::Machinist).to receive(:factories).and_return([@machinist = double('machinist', :name => 'machinist')])
expect(Pickle::Adapter::FactoryGirl).to receive(:factories).and_return([@factory_girl = double('factory_girl', :name => 'factory_girl')])
expect(Pickle::Adapter::FactoryBot).to receive(:factories).and_return([@factory_bot = double('factory_bot', :name => 'factory_bot')])
expect(Pickle::Adapter::Fabrication).to receive(:factories).and_return([@fabrication = double('fabrication', :name => 'fabrication')])
expect(Pickle::Adapter::Orm).to receive(:factories).and_return([@orm = double('orm', :name => 'orm')])
expect(@config.factories).to eq({'machinist' => @machinist, 'factory_girl' => @factory_girl, 'fabrication' => @fabrication, 'orm' => @orm})
expect(@config.factories).to eq({'machinist' => @machinist, 'factory_bot' => @factory_bot, 'fabrication' => @fabrication, 'orm' => @orm})
end

it "should give preference to adaptors first in the list" do
expect(Pickle::Adapter::Machinist).to receive(:factories).and_return([@machinist_one = double('one', :name => 'one')])
expect(Pickle::Adapter::FactoryGirl).to receive(:factories).and_return([@factory_girl_one = double('one', :name => 'one'), @factory_girl_two = double('two', :name => 'two')])
expect(Pickle::Adapter::FactoryBot).to receive(:factories).and_return([@factory_bot_one = double('one', :name => 'one'), @factory_bot_two = double('two', :name => 'two')])
expect(Pickle::Adapter::Fabrication).to receive(:factories).and_return([@fabrication_one = double('one', :name => 'one'), @fabrication_three = double('three', :name => 'three')])
expect(Pickle::Adapter::Orm).to receive(:factories).and_return([@orm_two = double('two', :name => 'two'), @orm_four = double('four', :name => 'four')])
expect(@config.factories).to eq({'one' => @machinist_one, 'two' => @factory_girl_two, 'three' => @fabrication_three, 'four' => @orm_four})
expect(@config.factories).to eq({'one' => @machinist_one, 'two' => @factory_bot_two, 'three' => @fabrication_three, 'four' => @orm_four})
end
end

@@ -300,10 +300,10 @@ class Model
let(:shirl) { double("shirl", :class => user_class, :id => 3) }
let(:noname) { double("noname", :class => user_class, :id => 4) }

if defined? ::FactoryGirl
let(:super_admin_factory) { Pickle::Adapter::FactoryGirl.new(double(:build_class => user_class, :name => :super_admin), :super_admin) }
if defined? ::FactoryBot
let(:super_admin_factory) { Pickle::Adapter::FactoryBot.new(double(:build_class => user_class, :name => :super_admin), :super_admin) }
else
let(:super_admin_factory) { Pickle::Adapter::FactoryGirl.new(double(:build_class => user_class, :factory_name => :super_admin), :super_admin) }
let(:super_admin_factory) { Pickle::Adapter::FactoryBot.new(double(:build_class => user_class, :factory_name => :super_admin), :super_admin) }
end

before do
@@ -1,7 +1,7 @@
require 'rails'
require 'active_support'
require 'active_record'
require 'factory_girl'
require 'factory_bot'

require 'simplecov'
require 'codecov'

0 comments on commit 41933f3

Please sign in to comment.