在Heroku上测试缓慢

时间:2018-12-13 18:52:10

标签: ruby-on-rails heroku rspec capybara

我可以理解这个问题很笼统,但是我遇到的一个问题是,我在Heroku上进行的测试非常慢(超过20分钟),而在本地运行它们大约需要9或10分钟。我想知道我是否可以针对这个问题指明正确的方向。这是我的rails_helper.rb

require 'simplecov'
SimpleCov.start
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?

require 'spec_helper'
require 'rspec/rails'
require 'shoulda/matchers'

require 'sidekiq/testing'
Sidekiq::Testing.fake!

require 'public_activity/testing'
PublicActivity.enabled = false

# Checks for pending migrations before tests are run.
ActiveRecord::Migration.maintain_test_schema!

require 'capybara/rspec'


Capybara.asset_host = ENV['CAPYBARA_ASSET_HOST'] || "http://localhost:#{ENV.fetch('PORT', 3000)}"
Capybara.register_driver :headless_chrome do |app|
  chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
  chrome_opts = chrome_bin ? { 'binary': chrome_bin } : {}
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome chromeOptions: { args: %w[headless disable-gpu disable-dev-shm-usage no-sandbox] }.merge(chrome_opts)
  Capybara::Selenium::Driver.new app, browser: :chrome, desired_capabilities: capabilities
end
Capybara.javascript_driver = :headless_chrome
Capybara.server = :puma, { Silent: true }

# NOTE: Running with sauce REQUIRES SAUCE CONNECT tunnel driver
# so that saucelabs instance can communicate with your test server.
# run with...
# bin/sc -u obiefernandez -k 24e2ec72-ff16-4526-9a35-d7e8bd180d5a
#
# require "selenium/webdriver"
# Capybara.register_driver :sauce do |app|
#   caps = Selenium::WebDriver::Remote::Capabilities.chrome()
#   caps['platform'] = 'macOS 10.12'
#   caps['version'] = '52.0'
#   sauce_endpoint = "http://obiefernandez:24e2ec72-ff16-4526-9a35-d7e8bd180d5a@ondemand.saucelabs.com:80/wd/hub"
#   Capybara::Selenium::Driver.new app, :browser => :remote, :url => sauce_endpoint, :desired_capabilities => caps
# end
# Capybara.javascript_driver = :sauce

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods

  config.before(:suite) do
    # compile front-end and load manifest
    `bin/webpack`
    Webpacker::Manifest.load
    DatabaseCleaner.allow_remote_database_url = true
    DatabaseCleaner.strategy = :truncation, { pre_count: true, except: %w(ar_internal_metadata) }
    DatabaseCleaner.clean
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

  config.backtrace_exclusion_patterns << %r{/rails_helper\.rb:}

  module SignInHelpers
    extend ActiveSupport::Concern
    class_methods do
      def managing_project(&block)
        if block_given?
          let!(:project, &block)
          let(:organization) { project.organization }
          let(:project_manager) { create(:user, organization: organization, organization_admin: true) }
        else
          let!(:organization) { create(:organization_with_project_manager) }
          let!(:project) { create(:project, organization: organization) }
          let(:project_manager) { organization.admins.first }
        end

        before do
          set_current_user(project_manager)
          sign_in(project_manager) if respond_to?(:sign_in)
        end
      end
    end
  end

  config.fixture_path = 'spec/fixtures'

  config.include Devise::Test::ControllerHelpers, type: :controller
  Devise::Test::ControllerHelpers.module_eval do
    alias_method :original_sign_in, :sign_in
    def sign_in(resource, deprecated = nil, scope: nil)
      original_sign_in(resource, scope: scope)
      # needed because devise integration helper bypasses normal ApplicationController logic
      set_current_user(resource)
    end
  end

  config.include Devise::Test::IntegrationHelpers, type: :feature
  Devise::Test::IntegrationHelpers.module_eval do
    alias_method :original_sign_in, :sign_in
    def sign_in(resource, scope: nil)
      original_sign_in(resource, scope: scope)
      # needed because devise integration helper bypasses normal ApplicationController logic
      set_current_user(resource)
    end
  end

  config.include SignInHelpers # needed everywhere

  # https://github.com/state-machines/state_machines-rspec
  config.include StateMachinesRspec::Matchers

  module IntegrationHelpers
    extend ActiveSupport::Concern
    included do
      subject { page }
    end

    def visit(url, *args)
      super(polymorphic_path(url), *args)
    rescue
      super
    end
  end

  config.before(:each) do
    Sidekiq::Worker.clear_all
  end

  config.include IntegrationHelpers, type: :feature
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

0 个答案:

没有答案