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