由于Bootstrap-Rails 4导致Rails测试失败

时间:2016-08-21 16:40:37

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

我正在关注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>'

我可以破译 - 它正在尝试使用bootstrap而不能。

也许我对TDD不够熟悉,但为什么要尝试使用bootstrap来渲染标题呢?是因为部分?

我发现了很多其他帖子都有同样的问题,但是大多数都要么死了,要么就是来自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

2 个答案:

答案 0 :(得分:0)

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

我认为问题是Git更新或无法更新某个文件的修改时间戳。我相信在运行rails test时,Rails资产管道中的预处理器引擎无法更新某些生成的文件。

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

答案 1 :(得分:0)

在合并到master分支之前,使用

快速检查正在运行的spring服务器
ps aux | grep spring

然后用

停止它
spring stop

pkill -15 -f spring