Skip to content

Commit

Permalink
Use FactoryBo#reload to reset factory bot
Browse files Browse the repository at this point in the history
fixes #66
  • Loading branch information
grantspeelman committed Jul 24, 2020
1 parent 8d8fdde commit 7cb55ac
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ spec/examples.txt
.idea
spec/test.log
pkg/*.gem
vendor/bundle

This comment has been minimized.

Copy link
@justin808

justin808 Jul 28, 2020

Member

@grantspeelman what's this vendor/bundle addition for?

This comment has been minimized.

Copy link
@grantspeelman

grantspeelman Jul 30, 2020

Author Collaborator

because I bundle install to that path

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### Changed
* Use `FactoryBo#reload` to reset factory bot

## [1.7.0]
[Compare]: https://github.com/shakacode/cypress-on-rails/compare/v1.6.0...v1.7.0

Expand Down
1 change: 1 addition & 0 deletions cypress-on-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'
s.add_development_dependency 'railties', '>= 3.2'
s.add_development_dependency 'factory_bot'
s.metadata = {
"bug_tracker_uri" => "https://github.com/shakacode/cypress-on-rails/issues",
"changelog_uri" => "https://github.com/shakacode/cypress-on-rails/blob/master/CHANGELOG.md",
Expand Down
4 changes: 4 additions & 0 deletions lib/cypress_on_rails/simple_rails_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ def self.create_list(type, amount, *params)
create(type,*params)
end
end

# to be API compatible with factorybot
def self.definition_file_paths=(*); end
def self.reload; end
end
end
16 changes: 3 additions & 13 deletions lib/cypress_on_rails/smart_factory_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@

module CypressOnRails
class SmartFactoryWrapper
module FactoryCleaner
def self.clean(f = FactoryBot)
f.factories.clear if f.respond_to?(:factories)
f.traits.clear if f.respond_to?(:traits)
f.callbacks.clear if f.respond_to?(:callbacks)
f.sequences.clear if f.respond_to?(:sequences)
end
end

def self.instance
@instance ||= new(files: [], factory: SimpleRailsFactory)
end
Expand All @@ -33,14 +24,13 @@ def self.create_list(*args)
attr_accessor :always_reload

def initialize(files:, factory:, always_reload: false,
factory_cleaner: FactoryCleaner, kernel: Kernel, file_system: File,
dir_system: Dir)
kernel: Kernel, file_system: File, dir_system: Dir)
self.files = files
self.factory = factory
factory.definition_file_paths = []
self.always_reload = always_reload
@kernel = kernel
@file_system = file_system
@factory_cleaner = factory_cleaner
@latest_mtime = nil
@dir_system = dir_system
end
Expand Down Expand Up @@ -83,7 +73,7 @@ def load_files
return unless should_reload?(current_latest_mtime)
logger.info 'Loading Factories'
@latest_mtime = current_latest_mtime
@factory_cleaner.clean(factory)
factory.reload
files.each do |file|
logger.debug "-- Loading: #{file}"
@kernel.load(file)
Expand Down
25 changes: 13 additions & 12 deletions spec/cypress_on_rails/smart_factory_wrapper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'cypress_on_rails/smart_factory_wrapper'
require 'factory_bot'

RSpec.describe CypressOnRails::SmartFactoryWrapper do
FileSystemDummy = Struct.new(:file_hash) do
Expand All @@ -10,8 +11,9 @@ def mtime(filename)
let(:time_now) { Time.now }
let(:mtime_hash) { {'file1.rb' => time_now, 'file2.rb' => time_now } }
let(:files) { %w(file1.rb file2.rb) }
let(:factory_double) { double('FactoryBot', create: true, create_list: true) }
let(:factory_cleaner) { class_double(CypressOnRails::SmartFactoryWrapper::FactoryCleaner, clean: true) }
let(:factory_double) do
class_double(FactoryBot, create: nil, create_list: nil, "definition_file_paths=": nil, reload: nil)
end
let(:kernel_double) { class_double(Kernel, load: true) }
let(:file_double) { FileSystemDummy.new(mtime_hash) }
let(:dir_double) { class_double(Dir) }
Expand All @@ -25,8 +27,7 @@ def mtime(filename)
factory: factory_double,
kernel: kernel_double,
file_system: file_double,
dir_system: dir_double,
factory_cleaner: factory_cleaner)
dir_system: dir_double)
end

it 'loads all the files on first create it' do
Expand Down Expand Up @@ -68,7 +69,7 @@ def mtime(filename)

it 'wont load the files if they have not changed' do
subject.create(:user)
subject.create_list(:user)
subject.create_list(:user, 2)
expect(kernel_double).to have_received(:load).with('file1.rb').once
expect(kernel_double).to have_received(:load).with('file2.rb').once
end
Expand All @@ -79,7 +80,7 @@ def mtime(filename)
expect(kernel_double).to have_received(:load).with('file2.rb').once

mtime_hash['file1.rb'] = Time.now
subject.create_list(:user)
subject.create_list(:user, 2)
expect(kernel_double).to have_received(:load).with('file1.rb').twice
expect(kernel_double).to have_received(:load).with('file2.rb').twice
end
Expand All @@ -91,24 +92,24 @@ def mtime(filename)
expect(kernel_double).to have_received(:load).with('file2.rb').once

mtime_hash.delete('file1.rb')
subject.create_list(:user)
subject.create_list(:user, 2)
expect(kernel_double).to have_received(:load).with('file1.rb').once
expect(kernel_double).to have_received(:load).with('file2.rb').twice
end

it 'will reset factory if a has changed' do
it 'will reset factory if a file has changed' do
subject.create(:user)
expect(factory_cleaner).to have_received(:clean).with(factory_double)
expect(factory_double).to have_received(:reload)

mtime_hash['file1.rb'] = Time.now
subject.create_list(:user)
subject.create_list(:user, 2)

expect(factory_cleaner).to have_received(:clean).with(factory_double).twice
expect(factory_double).to have_received(:reload).twice
end

it 'will always reload the files enabled' do
subject.always_reload = true
subject.create_list(:user)
subject.create_list(:user, 2)
subject.create(:user)
expect(kernel_double).to have_received(:load).with('file1.rb').twice
expect(kernel_double).to have_received(:load).with('file2.rb').twice
Expand Down

0 comments on commit 7cb55ac

Please sign in to comment.