我无法弄清楚!任何帮助将不胜感激。
摘要
我的RSpec测试失败,但是该应用在开发中运行良好。
测试失败表明资产(图像)不在管道中。问题在于图像在管道中,并且应用程序在开发中正确显示了图像。
该应用程序和RSpec测试在Rails的早期版本(4.2和5.0)中工作
错误
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/ruby-2.6.4@rails5.2.3/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/ruby-2.6.4@rails5.2.3/gems/warden-1.2.8/lib/warden/manager.rb:36:in `block in call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/warden-1.2.8/lib/warden/manager.rb:34:in `catch'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/warden-1.2.8/lib/warden/manager.rb:34:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/tempfile_reaper.rb:15:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/etag.rb:25:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/conditional_get.rb:25:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/head.rb:12:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:232:in `context'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:226:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/railties-5.2.3/lib/rails/rack/logger.rb:38:in `call_app'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/railties-5.2.3/lib/rails/rack/logger.rb:26:in `block in call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/railties-5.2.3/lib/rails/rack/logger.rb:26:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/runtime.rb:22:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/railties-5.2.3/lib/rails/engine.rb:524:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/urlmap.rb:68:in `block in call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/urlmap.rb:53:in `each'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-2.0.7/lib/rack/urlmap.rb:53:in `call'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-test-1.1.0/lib/rack/test.rb:129:in `custom_request'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/rack-test-1.1.0/lib/rack/test.rb:58:in `get'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/capybara-3.28.0/lib/capybara/rack_test/browser.rb:65:in `process'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/capybara-3.28.0/lib/capybara/rack_test/browser.rb:43:in `process_and_follow_redirects'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/capybara-3.28.0/lib/capybara/rack_test/browser.rb:23:in `visit'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/capybara-3.28.0/lib/capybara/rack_test/driver.rb:45:in `visit'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/gems/capybara-3.28.0/lib/capybara/session.rb:276:in `visit'
# /Users/cw/.rvm/gems/ruby-2.6.4@rails5.2.3/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/ruby-2.6.4@rails5.2.3/gems/sprockets-rails-3.2.1/lib/sprockets/rails/helper.rb:83:in `compute_asset_path'
查看:html.erb代码
image_tag("lendersbig/#{@lender.image_file_big}")
检查浏览器中的元素
图像通过管道运行,即已添加指纹
<img src="/assets/lendersbig/loanstore-big-eb95ff2644927e978748f13dd30bfc99.png">
文件夹结构
app
--assets
-- images
-- lendersbig
-- <files>
宝石和Ruby版本
ruby "2.6.4"
gem 'rails', '5.2.3'
gem 'rspec-rails','3.8.2'
经过尝试和/或研究的事物
我在文件名前加了一个“ /”,测试通过了,但是在应用运行时图像没有显示。
image_tag("/lendersbig/#{@lender.image_file_big}")
我尝试使用“ skip_pipeline:true”。测试通过,但应用程序失败-图像损坏。
image_tag("lendersbig/#{@lender.image_file_big}", skip_pipeline: true)
我认为我不需要使用“ config / initializers / assets.rb”,因为图像位于默认的管道路径中。
我在sprocket-rails中发现了描述此问题的线程。似乎还没有解决...
其他信息
此应用可在生产环境中使用,并在Rail 4.2.11和Rails 5.0.7.2中通过RSpec测试
RSpec在Rails 5.1.0中确实针对此问题引发了弃用警告,但是我在路径前面添加了“ /”,并错误地认为它已解决。后来,我运行了该应用程序并意识到图像链接已断开,即图像路径不正确。这是从Rails 5.0.7.2到5.1.0的捆绑软件更新,导致不建议使用的警告:“资产XYZ在资产管道中不存在。”在Rails 5.2中,测试如上所述失败。希望这可以激发一些想法...
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
结论
因此,我觉得我做的事情确实很愚蠢,缺少某些东西(很有可能),或者与Sprockets与Rails的集成有关。感谢您的任何建议!
答案 0 :(得分:0)
我发现了问题-在测试环境管道中没有图像。
在此应用程序中,每个图像都附加到数据库中的一条记录,并且仅在显示该记录时调用(可以使用Rails5中的ActiveStorage做到这一点)。图像存储在管道中,因此一切都在开发和生产中起作用。在测试中,调用图像时没有测试图像。
要解决此问题,我调整了工厂
# spec/factories/lender.rb
FactoryBot.define do
factory :lender do
# other attributes
sequence(:image_file_big) { |n| "test-image" }
end
end
,然后将图像文件添加到../lendersbig/test-image.png
事后看来,这过去应该会失败,但是sprockets-rails
gem的升级可能会导致现在正确失败。
如果有更好的方法,请告诉我。谢谢!
答案 1 :(得分:0)
在 Rails 6.1.3.1 中,我在生产模式(不是在开发模式下)遇到了这个问题,我比较了 config/environments/development.rb 和 config/environments/production.rb 并推断更改以下设置从假到真解决问题
config.assets.compile = true