Search code examples
ruby-on-railsrspecruby-on-rails-5asset-pipelinesprockets

Rails 5.2.3 -- ActionView::Template::Error: The asset is not present in the asset pipeline


I cannot figure this out! Any help would be greatly appreciated.

Summary

After upgrading to Rails 5.2.3, my RSpec test fails but the app works fine in development.

The test failure states the asset, an image, is not in the pipeline. The problem is that the image is in the pipeline and the app displays the image correctly in development.

The app and RSpec tests work in previous versions of Rails (4.2 and 5.0)

Error

Failures:

1) Lender Pages Show Lender Pages Term Lender should show term lenders details
Failure/Error: <div class = "lenderImage"><%= image_tag("lendersbig/#{@lender.image_file_big}", :alt => @lender.name )  %></div>

 ActionView::Template::Error:
   The asset "lendersbig/image1.png" is not present in the asset pipeline.
 # /Users/cw/.rvm/gems/[email protected]/gems/sprockets-rails-3.2.1/lib/sprockets/rails/helper.rb:83:in `compute_asset_path'
 # ./app/views/lenders/_lender_introbox.html.erb:27:in `_app_views_lenders__lender_introbox_html_erb__4253291031052859852_70308380940160'
 # ./app/views/lenders/show.html.erb:33:in `_app_views_lenders_show_html_erb__24939295181715536_70308376642600'
 # /Users/cw/.rvm/gems/[email protected]/gems/warden-1.2.8/lib/warden/manager.rb:36:in `block in call'
 # /Users/cw/.rvm/gems/[email protected]/gems/warden-1.2.8/lib/warden/manager.rb:34:in `catch'
 # /Users/cw/.rvm/gems/[email protected]/gems/warden-1.2.8/lib/warden/manager.rb:34:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/tempfile_reaper.rb:15:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/etag.rb:25:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/conditional_get.rb:25:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/head.rb:12:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:232:in `context'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:226:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/railties-5.2.3/lib/rails/rack/logger.rb:38:in `call_app'
 # /Users/cw/.rvm/gems/[email protected]/gems/railties-5.2.3/lib/rails/rack/logger.rb:26:in `block in call'
 # /Users/cw/.rvm/gems/[email protected]/gems/railties-5.2.3/lib/rails/rack/logger.rb:26:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/runtime.rb:22:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/railties-5.2.3/lib/rails/engine.rb:524:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/urlmap.rb:68:in `block in call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/urlmap.rb:53:in `each'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-2.0.7/lib/rack/urlmap.rb:53:in `call'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-test-1.1.0/lib/rack/test.rb:129:in `custom_request'
 # /Users/cw/.rvm/gems/[email protected]/gems/rack-test-1.1.0/lib/rack/test.rb:58:in `get'
 # /Users/cw/.rvm/gems/[email protected]/gems/capybara-3.28.0/lib/capybara/rack_test/browser.rb:65:in `process'
 # /Users/cw/.rvm/gems/[email protected]/gems/capybara-3.28.0/lib/capybara/rack_test/browser.rb:43:in `process_and_follow_redirects'
 # /Users/cw/.rvm/gems/[email protected]/gems/capybara-3.28.0/lib/capybara/rack_test/browser.rb:23:in `visit'
 # /Users/cw/.rvm/gems/[email protected]/gems/capybara-3.28.0/lib/capybara/rack_test/driver.rb:45:in `visit'
 # /Users/cw/.rvm/gems/[email protected]/gems/capybara-3.28.0/lib/capybara/session.rb:276:in `visit'
 # /Users/cw/.rvm/gems/[email protected]/gems/capybara-3.28.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
 # ./spec/requests/lenders_pages_spec.rb:31:in `block (4 levels) in <top (required)>'
 # ------------------
 # --- Caused by: ---
 # Sprockets::Rails::Helper::AssetNotFound:
 #   The asset "lendersbig/image1.png" is not present in the asset pipeline.
 #   /Users/cw/.rvm/gems/[email protected]/gems/sprockets-rails-3.2.1/lib/sprockets/rails/helper.rb:83:in `compute_asset_path'

View: html.erb Code

image_tag("lendersbig/#{@lender.image_file_big}")

Inspect Element in Browerser

The image is run through the pipeline, i.e. the fingerprint has been added

<img src="/assets/lendersbig/loanstore-big-eb95ff2644927e978748f13dd30bfc99.png">

Folder Structure

app
--assets
  -- images
    -- lendersbig
      -- <files>

Gem and Ruby Versions

ruby "2.6.4"        
gem 'rails', '5.2.3'
gem 'rspec-rails','3.8.2'

Things Tried and/or Researched

  1. I put a "/" in front of the filename and the test passes but the image doesn't show up when the app is running.

    image_tag("/lendersbig/#{@lender.image_file_big}")
    
  2. I tried using "skip_pipeline: true". Test passes but app fails -- broken images.

    image_tag("lendersbig/#{@lender.image_file_big}", skip_pipeline: true)
    
  3. I don't think I need to play with "config/initializers/assets.rb" since the images are in the default pipeline path.

  4. I found this thread in sprocket-rails that describes this problem. It doesn't seem to have been fixed...

Additional Information

This app works in production and passes RSpec tests in Rail 4.2.11 and Rails 5.0.7.2

RSpec did throw deprecation warnings for this issue in Rails 5.1.0 but I added the "/" in front of the path and incorrectly thought it was solved. Later, I ran the app and realized that the image links were broken, i.e. the image path was not correct. This is the bundle update from Rails 5.0.7.2 to 5.1.0 that resulted in the deprecations warnings, "The asset XYZ is not present in the asset pipeline." In Rails 5.2 the tests fail as above. Hopefully, this helps spark some ideas...

Using rake 12.3.3
Using concurrent-ruby 1.1.5
Using i18n 0.9.5
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 5.1.0 (was 5.0.7.2)
Using builder 3.2.3
Using erubi 1.8.0
Using mini_portile2 2.4.0
Using nokogiri 1.10.4
Using rails-dom-testing 2.0.3
Using crass 1.0.4
Using loofah 2.2.3
Using rails-html-sanitizer 1.2.0
Using actionview 5.1.0 (was 5.0.7.2)
Using rack 2.0.7
Using rack-test 0.6.3
Using actionpack 5.1.0 (was 5.0.7.2)
Using nio4r 2.5.1
Using websocket-extensions 0.1.4
Using websocket-driver 0.6.5
Using actioncable 5.1.0 (was 5.0.7.2)
Using globalid 0.4.2
Using activejob 5.1.0 (was 5.0.7.2)
Using mini_mime 1.0.2
Using mail 2.7.1
Using actionmailer 5.1.0 (was 5.0.7.2)
Using arbre 1.2.1
Using formtastic 3.1.5
Using formtastic_i18n 0.6.0
Using has_scope 0.7.2
Using method_source 0.9.2
Using thor 0.20.3
Using railties 5.1.0 (was 5.0.7.2)
Using responders 3.0.0
Using inherited_resources 1.11.0
Using jquery-rails 4.3.5
Using kaminari-core 1.1.1
Using kaminari-actionview 1.1.1
Using activemodel 5.1.0 (was 5.0.7.2)
Using arel 8.0.0 (was 7.1.4)
Using activerecord 5.1.0 (was 5.0.7.2)
Using kaminari-activerecord 1.1.1
Using kaminari 1.1.1
Using polyamorous 2.3.0
Using ransack 2.3.0
Using ffi 1.11.1
Using sassc 2.2.0
Using sprockets 3.7.2
Using sprockets-rails 3.2.1
Using tilt 2.0.9
Using sassc-rails 2.1.2
Using babel-source 5.8.35
Using execjs 2.0.2
Using babel-transpiler 0.7.0
Using sprockets-es6 0.9.2
Using activeadmin 2.2.0
Using public_suffix 4.0.1
Using addressable 2.7.0
Using json 1.8.6
Using uuidtools 2.1.5
Using aws-sdk 1.38.0
Using bcrypt 3.1.13
Using bindex 0.8.1
Using bundler 1.17.3
Using byebug 11.0.1
Using regexp_parser 1.6.0
Using xpath 3.2.0
Using capybara 3.28.0
Using childprocess 2.0.0
Using climate_control 0.2.0
Using cocaine 0.5.8
Using coderay 1.1.2
Using coffee-script-source 1.12.2
Using coffee-script 2.4.1
Using coffee-rails 4.2.2
Using database_cleaner 1.7.0
Using orm_adapter 0.5.0
Using warden 1.2.8
Using devise 4.7.0
Using diff-lcs 1.3
Using factory_bot 5.0.2
Using factory_bot_rails 5.0.2
Using faker 1.4.3
Using figaro 1.1.1
Using jbuilder 2.9.1
Using jquery-ui-rails 5.0.5
Using launchy 2.4.2
Using libv8 3.16.14.19 (x86_64-darwin-18)
Using rb-fsevent 0.10.3
Using rb-inotify 0.10.0
Using ruby_dep 1.5.0
Using listen 3.1.5
Using mime-types-data 3.2019.0904
Using mime-types 3.3
Using paperclip 4.1.1
Using pg 0.21.0
Using slop 3.6.0
Using pry 0.9.12.6
Using pry-nav 0.2.3
Using puma 3.12.0
Using rails 5.1.0 (was 5.0.7.2)
Using rails-controller-testing 1.0.4
Using rails_serve_static_assets 0.0.5
Using rails_stdout_logging 0.0.5
Using rails_12factor 0.0.2
Using ref 2.0.0
Using rspec-support 3.8.2
Using rspec-core 3.8.2
Using rspec-expectations 3.8.4
Using rspec-mocks 3.8.1
Using rspec-rails 3.8.2
Using rubyzip 1.2.4
Using sass-rails 6.0.0
Using selenium-webdriver 3.142.4
Using shoulda-context 1.2.2
Using shoulda-matchers 2.8.0
Using shoulda 3.5.0
Using spring 2.1.0
Using spring-commands-rspec 1.0.4
Using spring-watcher-listen 2.0.1
Using therubyracer 0.12.3
Using turbolinks-source 5.2.0
Using turbolinks 5.2.0
Using uglifier 4.1.20
Using web-console 3.7.0
Using webdrivers 4.1.2

Conclusion

So I feel like either I'm doing something really dumb and missing something (most likely) or it's something to do with the Sprockets integration with Rails. Thanks for any suggestions!


Solution

  • I figured out the problem -- there was no image in the test environment pipeline.

    In this app, each image is attached to a record in the database and only called when that record is displayed (think this can be be done using ActiveStorage in Rails5). The images are stored in the pipeline so everything worked in development and production. In testing, when the image was called there was no test image.

    To fix this I adjusted the factory

    # spec/factories/lender.rb
    FactoryBot.define do
      factory :lender do
        # other attributes
        image_file_big { "test-image" }
      end
    end
    

    and then added an image file to ../lendersbig/test-image.png

    In hindsight, this should have failed in the past but maybe upgrades in the sprockets-rails gem caused this to correctly fail now.

    If there is a better way to do this let me know. Thanks!