【问题标题】:uninitialized constant ActionView::Template::Handlers::ERB::ENCODING_FLAG未初始化的常量 ActionView::Template::Handlers::ERB::ENCODING_FLAG
【发布时间】:2018-08-26 01:30:20
【问题描述】:

我目前正在开发Rails 应用程序版本Rails 4.2.6ruby 2.2.2。我正在尝试运行 rspec 测试,我有版本

 rspec-core 3.7.1
  - rspec-expectations 3.7.0
  - rspec-mocks 3.7.0
  - rspec-rails 3.7.2
  - rspec-support 3.7.1

当我运行以下命令 bundle exec rspec spec/models/user_spec.rb 时,我收到以下错误: 加载 ./spec/models/user_spec.rb 时出错。

失败/错误:需要“rspec/rails”

NameError: 未初始化的常量 ActionView::Template::Handlers::ERB::ENCODING_FLAG

这是我的 rails_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
require "rspec/rails"
require "capybara/rspec"

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?

ActiveRecord::Migration.maintain_test_schema!

Capybara.register_driver :selenium_chrome do |app|
 Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.javascript_driver = :selenium_chrome

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.use_transactional_fixtures = false

  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

  # This block must be here, do not combine with the other `before(:each)` block.
  # This makes it so Capybara can see the database.
  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

这是 Gemfile:

group :development, :test do
  gem 'rspec'
  gem 'rspec-rails'
  gem 'byebug'
  gem 'pry'
  gem 'pry-nav'
  gem 'pry-stack_explorer'
end

group :test do
  gem "capybara"
  gem "selenium-webdriver"
end

【问题讨论】:

    标签: ruby-on-rails-4 rspec rspec-rails


    【解决方案1】:

    如果你移动会发生什么:

    ...
    ENV['RAILS_ENV'] ||= 'test'
    require File.expand_path('../../config/environment', __FILE__)
    ...
    

    到文件的开头?

    【讨论】:

    • require File.expand_path('../../config/environment', __FILE__) btw 的工作是什么?
    • 它配置/初始化一些库所依赖的某些公共常量
    猜你喜欢
    • 2014-04-28
    • 1970-01-01
    • 2012-04-29
    • 2015-12-29
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多