From f84150362b5aa29aea161c209095f24f2e99ccd0 Mon Sep 17 00:00:00 2001 From: Aditya Prakash Date: Fri, 19 Feb 2016 10:59:50 +0530 Subject: [PATCH 1/3] Move FactoryGirl.lint to rake task This significantly improves start time of tests. It is also the recommended way: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#linting-factories Read more at: https://github.com/thoughtbot/factory_girl/issues/772 --- lib/tasks/factory_girl.rake | 15 +++++++++++++++ spec/support/factory_girl.rb | 7 ------- 2 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 lib/tasks/factory_girl.rake delete mode 100644 spec/support/factory_girl.rb diff --git a/lib/tasks/factory_girl.rake b/lib/tasks/factory_girl.rake new file mode 100644 index 00000000..bccc1b66 --- /dev/null +++ b/lib/tasks/factory_girl.rake @@ -0,0 +1,15 @@ +namespace :factory_girl do + desc "Verify that all FactoryGirl factories are valid" + task lint: :environment do + if Rails.env.test? + begin + DatabaseCleaner.start + FactoryGirl.lint + ensure + DatabaseCleaner.clean + end + else + system("bundle exec rake factory_girl:lint RAILS_ENV='test'") + end + end +end diff --git a/spec/support/factory_girl.rb b/spec/support/factory_girl.rb deleted file mode 100644 index 031f2a19..00000000 --- a/spec/support/factory_girl.rb +++ /dev/null @@ -1,7 +0,0 @@ -RSpec.configure do |config| - - config.before(:suite) do - FactoryGirl.lint - end - -end From 640be7497fe1e5bba72d4609a7644ae4a9c161b8 Mon Sep 17 00:00:00 2001 From: Aditya Prakash Date: Sat, 20 Feb 2016 13:36:40 +0530 Subject: [PATCH 2/3] Add config to allow running FactoryGirl lint before suit By default the lint will be run before every test suit. Set it to false in config.yml if you would rather just use the rake task: `bundle exec rake factory_girl:lint` --- config/config.yml.example | 4 ++++ spec/support/factory_girl.rb | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 spec/support/factory_girl.rb diff --git a/config/config.yml.example b/config/config.yml.example index 86500145..226cc4b5 100644 --- a/config/config.yml.example +++ b/config/config.yml.example @@ -15,6 +15,10 @@ defaults: &defaults ichain: enabled: false + # Set it to false if you don't want FactoryGirl lint to run before every test suit. + # You can run lint manually with: bundle exec rake factory_girl:lint + factory_girl_lint: true + development: <<: *defaults diff --git a/spec/support/factory_girl.rb b/spec/support/factory_girl.rb new file mode 100644 index 00000000..51f4037c --- /dev/null +++ b/spec/support/factory_girl.rb @@ -0,0 +1,7 @@ +RSpec.configure do |config| + + config.before(:suite) do + FactoryGirl.lint if CONFIG['factory_girl_lint'] + end + +end From 63010724847dbfe21cb5ccdc678f2974721749f7 Mon Sep 17 00:00:00 2001 From: Aditya Prakash Date: Sat, 20 Feb 2016 20:08:28 +0530 Subject: [PATCH 3/3] Add webmock for external requests in tests external_request.rb needs to be required before lint is run. --- Gemfile | 2 ++ Gemfile.lock | 7 +++++++ lib/tasks/factory_girl.rake | 3 +++ spec/support/external_request.rb | 29 +++++++++++++++++++++++++++++ spec/support/factory_girl.rb | 7 ++++++- 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 spec/support/external_request.rb diff --git a/Gemfile b/Gemfile index c7f6a766..f2410c08 100644 --- a/Gemfile +++ b/Gemfile @@ -179,6 +179,8 @@ group :test do # Extracted from RSpec 3 stub_model and mock_model gem 'rspec-activemodel-mocks' gem 'timecop' + # Mock external requests + gem 'webmock' end group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index 8a6cc996..b4ef60a4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -130,6 +130,8 @@ GEM simplecov (>= 0.7) term-ansicolor thor + crack (0.4.2) + safe_yaml (~> 1.0.0) currencies (0.4.2) daemons (1.1.9) database_cleaner (1.3.0) @@ -409,6 +411,7 @@ GEM ruby-openid (2.5.0) ruby-progressbar (1.7.5) rubyzip (1.0.0) + safe_yaml (1.0.4) sass (3.2.19) sass-rails (4.0.4) railties (>= 4.0.0, < 5.0) @@ -464,6 +467,9 @@ GEM binding_of_caller (>= 0.7.2) railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) + webmock (1.20.4) + addressable (>= 2.3.6) + crack (>= 0.3.2) websocket-driver (0.3.3) whenever (0.9.2) activesupport (>= 2.3.4) @@ -553,6 +559,7 @@ DEPENDENCIES turbolinks uglifier (>= 1.3.0) web-console (~> 2.0) + webmock whenever BUNDLED WITH diff --git a/lib/tasks/factory_girl.rake b/lib/tasks/factory_girl.rake index bccc1b66..b922b0ef 100644 --- a/lib/tasks/factory_girl.rake +++ b/lib/tasks/factory_girl.rake @@ -1,9 +1,12 @@ +require_relative '../../spec/support/external_request' + namespace :factory_girl do desc "Verify that all FactoryGirl factories are valid" task lint: :environment do if Rails.env.test? begin DatabaseCleaner.start + mock_commercial_request FactoryGirl.lint ensure DatabaseCleaner.clean diff --git a/spec/support/external_request.rb b/spec/support/external_request.rb new file mode 100644 index 00000000..539fe701 --- /dev/null +++ b/spec/support/external_request.rb @@ -0,0 +1,29 @@ +# Mock external requests to youtube +require 'webmock/rspec' +WebMock.disable_net_connect!(allow_localhost: true) + +RSpec.configure do |config| + config.before(:each) do + mock_commercial_request + end +end + +def mock_commercial_request + response = { + author_name: 'Confreaks', + html: '', + thumbnail_width: 480, + thumbnail_url: 'https://i.ytimg.com/vi/BTTygyxuGj8/hqdefault.jpg', + provider_name: 'YouTube', + width: 459, + type: 'video', + provider_url: 'http://www.youtube.com/', + version: '1.0', + thumbnail_height: 360, + title: 'RailsConf 2014 - Closing Keynote by Aaron Patterson', + author_url: 'https://www.youtube.com/user/Confreaks', + height: 344 + } + WebMock.stub_request(:get, /.*youtube.*/). + to_return(status: 200, body: response.to_json, headers: {}) +end diff --git a/spec/support/factory_girl.rb b/spec/support/factory_girl.rb index 51f4037c..95d350ef 100644 --- a/spec/support/factory_girl.rb +++ b/spec/support/factory_girl.rb @@ -1,7 +1,12 @@ +require_relative 'external_request' + RSpec.configure do |config| config.before(:suite) do - FactoryGirl.lint if CONFIG['factory_girl_lint'] + if CONFIG['factory_girl_lint'] + mock_commercial_request + FactoryGirl.lint + end end end