sprockets-rails icon indicating copy to clipboard operation
sprockets-rails copied to clipboard

3.2.0 doesn't work in RSpec

Open coding-red-panda opened this issue 9 years ago • 5 comments

Hello,

I'm currently trying to upgrade our sprockets-rails from 3.1 to 3.2.0, and this works for the application itself, namely I can run the application and everything works, but our specs seem to be failing due missing assets.

This is the configuration we have set in our application.rb:

    # Assets configuration for Sprockets.
    # We don't allow fallbacks to detect assets not in the pipeline, we suppress the log file for requests
    # The specific environments can add additional configurations as needed.
    config.assets.unknown_asset_fallback = false
    config.assets.quiet = true

Our test.rb environment settings:

# frozen_string_literal: true
Riskmethods::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # The test environment is used exclusively to run your application's
  # test suite. You never need to work with it otherwise. Remember that
  # your test database is "scratch space" for the test suite and is wiped
  # and recreated between test runs. Don't rely on the data there!
  config.cache_classes = true

  # Configure static asset server for tests with Cache-Control for performance
  config.serve_static_files = true
  config.static_cache_control = 'public, max-age=3600'

  # Log error messages when you accidentally call methods on nil
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local = true
  config.action_controller.perform_caching = false

  # Sprocket whines in test if this is set to true.
  config.assets.compile = false

  # Raise exceptions instead of rendering exception templates
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment
  config.action_controller.allow_forgery_protection = false

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_mailer.delivery_method = :test
  config.action_mailer.default_url_options = { host: 'localhost:3000' }

  # Raise exception on mass assignment protection for Active Record models
  # Default behaviour in Rails 4, no longer configurable
  # config.active_record.mass_assignment_sanitizer = :strict

  # Print deprecation notices to the stderr
  config.active_support.deprecation = :stderr

  config.middleware.use RackSessionAccess::Middleware

  # Rails 4 new configuration settings
  config.eager_load = false
  config.active_record.raise_in_transactional_callbacks = true

  # Log Settings
  config.colorize_logging = true
  config.log_level = :debug
end

And this is the error being thrown out by RSpec:

Randomized with seed 29007
................................................................F.................................................................................................................................^C
RSpec is shutting down and will print the summary report... Interrupt again to force quit.
.

Failures:

  1) Admins::Import::IndicatorMessagesController when logged in renders the view properly
     Failure/Error: <%= javascript_include_tag :application, :admin, :map %>

     ActionView::Template::Error:
       The asset "application.js" is not present in the asset pipeline.

However, when I look at our config/initializes/assets.rb, the file is clearly present there.

coding-red-panda avatar Sep 26 '16 07:09 coding-red-panda

fixed it. Problem was that our compile was disabled in test for legacy reasons.... Workign like a charm now

coding-red-panda avatar Sep 26 '16 07:09 coding-red-panda

Sorry,

need to re-open this. By setting the compile = true I can get our specs working, for anythign related to .js/css file, but now they fail because none of the images are found inside the pipeline.

Am I required in this version to manually add every image we use to our config/initializers/assets.rb ? Or are we supposed to place them back in the public folder?

coding-red-panda avatar Sep 26 '16 08:09 coding-red-panda

error produced for example:

  2) Api::V1::MapsController#world_map renders the world map
     Failure/Error: iconUrl: "<%= image_path('map/pin_delivery.png') %>",

     ActionView::Template::Error:
       The asset "map/pin_delivery.png" is not present in the asset pipeline.

Aren't all images supposed to be by default in the assets pipeline?

coding-red-panda avatar Sep 26 '16 08:09 coding-red-panda

Additional Information:

Currently, I've succeeded in making our application work, as well as the RSpec tests, but I have no idea what is actually going wrong. The first thing I did was adding this snippet to our config/initializers/assets.rb:

# HACK: Add all images from our asset folders
#
# For some reason, Sprockets is not loading the images by default into the path.
# This hack adds all .png and .gif images to the initializer so they are part of the pipeline.
# Bug is opened to address this issue: https://github.com/rails/sprockets-rails/issues/379
#
%w(app vendor lib).each do |folder|
  folder_path = Rails.root.join(folder, 'assets', 'images')

  %w(*.png *.gif *.jpg).each do |extension|
    full_path = File.join(folder_path, '**', extension)

    Dir.glob(full_path).each do |entry|
      if entry.end_with?(extension.gsub('*', '')) # Kick out . entries
        Rails.application.config.assets.precompile << entry.gsub(folder_path.to_s + '/', '')
      end
    end
  end
end

The second step is using this configuration for our config/environments/test.rb:

# frozen_string_literal: true
Riskmethods::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # The test environment is used exclusively to run your application's
  # test suite. You never need to work with it otherwise. Remember that
  # your test database is "scratch space" for the test suite and is wiped
  # and recreated between test runs. Don't rely on the data there!
  config.cache_classes = true

  # Configure static asset server for tests with Cache-Control for performance
  config.serve_static_files = true
  config.static_cache_control = 'public, max-age=3600'

  # Log error messages when you accidentally call methods on nil
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local = true
  config.action_controller.perform_caching = false

  # Allow Asset debugging for clearer paths in the output
  config.assets.compile = true
  config.assets.digest = false

  # Raise exceptions instead of rendering exception templates
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment
  config.action_controller.allow_forgery_protection = false

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_mailer.delivery_method = :test
  config.action_mailer.default_url_options = { host: 'localhost:3000' }

  # Raise exception on mass assignment protection for Active Record models
  # Default behaviour in Rails 4, no longer configurable
  # config.active_record.mass_assignment_sanitizer = :strict

  # Print deprecation notices to the stderr
  config.active_support.deprecation = :stderr

  config.middleware.use RackSessionAccess::Middleware

  # Rails 4 new configuration settings
  config.eager_load = false
  config.active_record.raise_in_transactional_callbacks = true

  # Log Settings
  config.colorize_logging = true
  config.log_level = :debug
end

coding-red-panda avatar Sep 26 '16 11:09 coding-red-panda

browsing through the open issues, might be the same problem as #303

coding-red-panda avatar Sep 27 '16 10:09 coding-red-panda