【问题标题】:Rails test failing due to Bootstrap - Rails 4由于 Bootstrap 导致 Rails 测试失败 - Rails 4
【发布时间】:2016-08-21 16:40:36
【问题描述】:

我正在关注this 教程,直到现在我还没有遇到任何重大问题,但是当我突然尝试运行测试(或 Guard)时,它突然开始失败。

每次失败都会提醒我

ERROR["test_should_get_home", StaticPagesControllerTest, 1.3221390806138515]
 test_should_get_home#StaticPagesControllerTest (1.32s)
ActionView::Template::Error:         ActionView::Template::Error: File to import not found or unreadable: bootstrap-sprockets.
        Load paths:
          /home/ubuntu/workspace/sample_app/app/assets/config
          /home/ubuntu/workspace/sample_app/app/assets/images
          /home/ubuntu/workspace/sample_app/app/assets/javascripts
          /home/ubuntu/workspace/sample_app/app/assets/stylesheets
          /home/ubuntu/workspace/sample_app/vendor/assets/javascripts
          /home/ubuntu/workspace/sample_app/vendor/assets/stylesheets
          /usr/local/rvm/gems/ruby-2.3.0/gems/jquery-rails-4.1.1/vendor/assets/javascripts
          /usr/local/rvm/gems/ruby-2.3.0/gems/coffee-rails-4.2.1/lib/assets/javascripts
          /usr/local/rvm/gems/ruby-2.3.0/gems/actioncable-5.0.0.1/lib/assets/compiled
          /usr/local/rvm/gems/ruby-2.3.0/gems/turbolinks-source-5.0.0/lib/assets/javascripts
            app/assets/stylesheets/custom.scss:1
            app/views/static_pages/home.html.erb:13:in `_app_views_static_pages_home_html_erb___4391039624438268474_46194740'
            test/controllers/static_pages_controller_test.rb:15:in `block in <class:StaticPagesControllerTest>'

我可以破译 - 它正在尝试使用引导程序但不能。

也许我对 TDD 不够熟悉,但为什么它试图让引导程序来呈现标题?是因为偏吗?

我发现许多其他帖子都存在同样的问题,但大多数帖子要么死了,要么来自 Rails 3,然后重新启动服务器就解决了。

我能够解决它,但修改了我的 Gemfile。我将 gem 'bootstrap-sass', '3.3.6' 从顶部移到了

group :assets, :test, :development do
    gem 'bootstrap-sass', '3.3.6'
end

块。我认为块外的任何宝石都是永久加载的,但不是这样吗?我以后也应该坚持这种风格吗?

(为了符号而放置整个 gemfile)

source 'https://rubygems.org'

gem 'rails',        '5.0.0.1'
gem 'puma',         '3.4.0'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.0.0'
gem 'coffee-rails', '4.2.1'
gem 'jquery-rails', '4.1.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.4.1'

group :assets, :test, :development do
    gem 'bootstrap-sass', '3.3.6'
end


group :development, :test do
  gem 'sqlite3', '1.3.11'
  gem 'byebug',  '9.0.0', platform: :mri
end

group :development do
  gem 'web-console',           '3.1.1'
  gem 'listen',                '3.0.8'
  gem 'spring',                '1.7.2'
  gem 'spring-watcher-listen', '2.0.0'
end

group :test do
  gem 'rails-controller-testing', '0.1.1'
  gem 'minitest-reporters',       '1.1.9'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
  gem 'pg', '0.18.4'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

编辑:根据要求添加两个测试。

应用测试

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all
  include ApplicationHelper

  # Add more helper methods to be used by all tests here...
end

具体测试

需要'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
    get contact_path
    assert_select "title", full_title("Contact")
    get signup_path
    assert_select "title", full_title("Sign-Up")
  end
end

【问题讨论】:

  • 您的问题始于app/assets/stylesheets/custom.scss:1 是否有导入指令?你的失败测试是什么样子的?
  • @slowjack2k 添加了确切的测试本身。

标签: ruby-on-rails ruby twitter-bootstrap rubygems


【解决方案1】:

在将filling-in-layout 分支合并到section 5.5 of the tutorial 中的master 后,我观察到了相同的ActionView::Template::Error: File to import not found or unreadable: bootstrap-sprockets. 症状。

我认为问题在于 Git 更新或未能更新某些文件的修改时间戳。我相信这会阻止 Rails 资产管道中的预处理器引擎在运行rails test 时更新某些生成的文件。

我通过运行 git ls-files | xargs touch 解决了这个问题。

【讨论】:

    【解决方案2】:

    在合并到 master 分支之前,快速检查一下正在运行的 spring 服务器

    ps aux | grep spring
    

    然后停止它

    spring stop
    

    或与

    pkill -15 -f spring
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      相关资源
      最近更新 更多