在Rails应用程序中配置spec文件夹时出现问题

时间:2013-10-14 03:03:35

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

我正在使用rspec练习Rails4中的TDD内容,并打算通过命令创建多个文件夹,例如factoriesmodelrequestscontroller。所以我将这一行添加到config/application.rb

config.generators do |g| 
  g.test_framework :rspec, 
    :model_specs => true, 
    :view_specs => false, 
    :helper_specs => false, 
    :routing_specs => false, 
    :controller_specs => true, 
    :request_specs => true 
  g.fixture_replacement :factory_girl, :dir => "spec/factories" 
end

然而。当我在命令行中运行rails g rspec:install时。系统返回错误:

/home/rayvonmice/workspace/kulu/config/application.rb:14:in `<top (required)>': undefined local variable or method `config' for main:Object (NameError)
    from /home/rayvonmice/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/commands.rb:44:in `require'
    from /home/rayvonmice/.rvm/gems/ruby-2.0.0-p247@global/gems/railties-4.0.0/lib/rails/commands.rb:44:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

所以我不明白config/application.rb文件有什么问题。这是否意味着我只能手动生成这些文件夹?

1 个答案:

答案 0 :(得分:0)

看起来你在config之外的地方调用了它。

尝试将代码移动到以:

开头的块中
module Kulu
  class Application < Rails::Application
  ...
  config.generators do |g| 
    g.test_framework :rspec, 
      :model_specs => true, 
      :view_specs => false, 
      :helper_specs => false, 
      :routing_specs => false, 
      :controller_specs => true, 
      :request_specs => true 
    g.fixture_replacement :factory_girl, :dir => "spec/factories" 
  end
  ...
end