From 611c8b78b8e9aa66fa5c82ca7a2671590066663d Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Sat, 2 Jun 2018 14:44:00 +0200 Subject: [PATCH] Setup factory_bot --- Gemfile | 5 ++++- Gemfile.lock | 8 ++++++++ Guardfile | 1 + lib/tasks/factory_bot.rake | 13 +++++++++++++ test/factories/.rubocop.yml | 4 ++++ test/test_helper.rb | 1 + 6 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/factory_bot.rake create mode 100644 test/factories/.rubocop.yml diff --git a/Gemfile b/Gemfile index 14498f3..d9768ad 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,8 @@ group :development, :test do gem 'binding_of_caller' gem 'pry-byebug' gem 'pry-rails' + + gem "factory_bot_rails" end group :development do @@ -61,7 +63,6 @@ group :development do gem 'annotate', require: false gem 'letter_opener_web' - gem "guard" gem "guard-minitest" @@ -78,6 +79,8 @@ group :test do # Easy installation and use of chromedriver to run system tests with Chrome gem 'chromedriver-helper' gem 'launchy' + + gem "database_cleaner" end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem diff --git a/Gemfile.lock b/Gemfile.lock index 423758d..ee624a2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -94,6 +94,7 @@ GEM coderay (1.1.2) concurrent-ruby (1.0.5) crass (1.0.4) + database_cleaner (1.7.0) debug_inspector (0.0.3) devise (4.4.3) bcrypt (~> 3.0) @@ -105,6 +106,11 @@ GEM devise (>= 4.4) erubi (1.7.1) execjs (2.7.0) + factory_bot (4.10.0) + activesupport (>= 3.0.0) + factory_bot_rails (4.10.0) + factory_bot (~> 4.10.0) + railties (>= 3.0.0) ffi (1.9.23) formatador (0.2.5) globalid (0.4.1) @@ -310,8 +316,10 @@ DEPENDENCIES capistrano3-puma capybara (>= 2.15, < 4.0) chromedriver-helper + database_cleaner devise (~> 4.4) devise-i18n (~> 1.6) + factory_bot_rails guard guard-minitest jbuilder (~> 2.5) diff --git a/Guardfile b/Guardfile index 566e5dc..38acbea 100644 --- a/Guardfile +++ b/Guardfile @@ -26,4 +26,5 @@ guard "minitest", spring: "bin/rails test" do watch(%r{^test/.+_test\.rb$}) watch(%r{^test/test_helper\.rb$}) { "test" } watch(%r{^test/fixtures/.+\.yml$}) { "test" } + watch(%r{^test/factories/.+\.rb$}) { "test" } end diff --git a/lib/tasks/factory_bot.rake b/lib/tasks/factory_bot.rake new file mode 100644 index 0000000..532c2a1 --- /dev/null +++ b/lib/tasks/factory_bot.rake @@ -0,0 +1,13 @@ +namespace :factory_bot do + desc "Verify that all FactoryBot factories are valid" + task lint: :environment do + if Rails.env.test? + DatabaseCleaner.cleaning do + FactoryBot.lint + end + else + system("bundle exec rake factory_bot:lint RAILS_ENV='test'") + fail if $CHILD_STATUS.exitstatus.nonzero? + end + end +end diff --git a/test/factories/.rubocop.yml b/test/factories/.rubocop.yml new file mode 100644 index 0000000..3a888e5 --- /dev/null +++ b/test/factories/.rubocop.yml @@ -0,0 +1,4 @@ +inherit_from: ../../.rubocop.yml + +Style/BlockDelimiters: + EnforcedStyle: line_count_based diff --git a/test/test_helper.rb b/test/test_helper.rb index 2c91312..849662b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,6 +12,7 @@ class ActiveSupport::TestCase Warden.test_mode! # Add more helper methods to be used by all tests here... + include FactoryBot::Syntax::Methods end Capybara.register_driver :headless_chrome do |app|