diff --git a/.gitignore b/.gitignore index a52569b..31a50fc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ spec/examples.txt .idea spec/test.log pkg/*.gem +vendor/bundle \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d22bae..d608dc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cypress-on-rails.gemspec b/cypress-on-rails.gemspec index bd182d7..33a4b2f 100644 --- a/cypress-on-rails.gemspec +++ b/cypress-on-rails.gemspec @@ -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", diff --git a/lib/cypress_on_rails/simple_rails_factory.rb b/lib/cypress_on_rails/simple_rails_factory.rb index 63d71d0..aff5b04 100644 --- a/lib/cypress_on_rails/simple_rails_factory.rb +++ b/lib/cypress_on_rails/simple_rails_factory.rb @@ -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 diff --git a/lib/cypress_on_rails/smart_factory_wrapper.rb b/lib/cypress_on_rails/smart_factory_wrapper.rb index 0a8669a..dfd52ec 100644 --- a/lib/cypress_on_rails/smart_factory_wrapper.rb +++ b/lib/cypress_on_rails/smart_factory_wrapper.rb @@ -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 @@ -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 @@ -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) diff --git a/spec/cypress_on_rails/smart_factory_wrapper_spec.rb b/spec/cypress_on_rails/smart_factory_wrapper_spec.rb index 28da7f8..9788c9a 100644 --- a/spec/cypress_on_rails/smart_factory_wrapper_spec.rb +++ b/spec/cypress_on_rails/smart_factory_wrapper_spec.rb @@ -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 @@ -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) } @@ -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 @@ -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 @@ -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 @@ -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