【问题标题】:Stuck On Rails Application卡在 Rails 应用程序上
【发布时间】:2016-04-28 07:20:02
【问题描述】:

这是终端中的问题:

    treehouse:~/projects/odot (master *) $ bin/rails generate model todo_item todo_list:references content:string -p
/home/treehouse/projects/odot/config/application.rb:23:in `<class:Application>': undefined method `configure' for RSpec:Module (NoMethodError)
    from /home/treehouse/projects/odot/config/application.rb:10:in `<module:Odot>'
    from /home/treehouse/projects/odot/config/application.rb:9:in `<top (required)>'
    from /home/treehouse/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/railties-4.0.1/lib/rails/commands.rb:43:in `require'
    from /home/treehouse/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/railties-4.0.1/lib/rails/commands.rb:43:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

这里是 odot/config/application.rb 文件:

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)

module Odot
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    RSpec.configure do |c|
        c.expose_current_running_example_as :example
    end

    RSpec.configure do |c|
        c.expose_current_running_example_as :example
    end
  end
end

这里是 bin/rails 文件:

#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application',  __FILE__)
require_relative '../config/boot'
require 'rails/commands'

我不知道如何访问 .rbenv/.... 文件。

我似乎无法解决问题所在。这些是启动我的 rails 待办事项列表应用程序时文件夹中的默认文件。我在三个不同的文件中做了几个 rspec 测试,在完成最后一个文件后,这不会让我继续执行任何其他任务。

这在运行 bin/rake 时会一直持续:

rake aborted!
NoMethodError: undefined method `configure' for RSpec:Module
/home/treehouse/projects/odot/config/application.rb:23:in `<class:Application>'
/home/treehouse/projects/odot/config/application.rb:10:in `<module:Odot>'
/home/treehouse/projects/odot/config/application.rb:9:in `<top (required)>'
/home/treehouse/projects/odot/Rakefile:4:in `require'
/home/treehouse/projects/odot/Rakefile:4:in `<top (required)>'

那么这里是 rakefile 文件:

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)

Odot::Application.load_tasks

我不知道发生了什么。运气好的话?干杯。

【问题讨论】:

  • 如果它以前可以正常工作,那么您的 rbenv 可能已更改。
  • 你可能是对的。我在创建规范测试时遇到了一些错误,如果可以解决问题,我会点击捆绑更新。结果我忘了在我的一个 rspec 测试中包含一个“do”。我不认为会是这样。我不确定我是否有不同版本的 rake。我不记得也无法弄清楚可能发生了什么变化。

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


【解决方案1】:

不要将 RSpec 配置放入 application.rbapplication.rb 用于 Rails 应用程序的核心配置,适用于所有环境。您可以使用它来设置诸如时区之类的东西,以及要在 Rails 堆栈中使用的中间件。

RSpec 应该放在 Gemfile 的测试组中,以便在不需要时不会加载它 - IE 在生产中。

gem 'something'

group :test do
  gem 'rspec-rails'
end

application.rb 还有一个非常重要的部分:

Bundler.require(:default, Rails.env)

这告诉 Bundler 加载默认组和当前“环境组”(开发、测试或生产)中的所有 gem。因此,如果您在生产环境中启动服务器,它会在RSpec.configure 线上爆炸,因为不需要(也不应该)RSpec gem。

Rspec 配置的正确位置是spec/spec_helper.rbspec/rails_helper.rb

【讨论】:

    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2021-08-21
    • 2015-09-03
    • 1970-01-01
    • 2019-10-18
    相关资源
    最近更新 更多