Rails未初始化常量RelationshipController(NameError)

3

我在遵循ruby.railstutorial.org的教程,对我的应用进行了一些微小的修改。我正在尝试运行relationships_controller_spec,但是我始终得到以下错误:

$ bundle exec rspec spec/controllers/relationships_controller_spec.rb
Exception encountered: #<NameError: uninitialized constant RelationshipsController>
backtrace:
/Users/JP2/Documents/Development/Ruby/rails_projects/iPray/spec/controllers/relationships_controller_spec.rb:3:in `<top (required)>'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `block in load_spec_files'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `map'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `load_spec_files'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:18:in `run'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/rspec/monkey/spork/test_framework/rspec.rb:5:in `run_tests'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/spork-0.9.0.rc8/lib/spork/run_strategy/forking.rb:13:in `block in run'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/spork-0.9.0.rc8/lib/spork/forker.rb:21:in `block in initialize'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/spork-0.9.0.rc8/lib/spork/forker.rb:18:in `fork'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/spork-0.9.0.rc8/lib/spork/forker.rb:18:in `initialize'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/spork-0.9.0.rc8/lib/spork/run_strategy/forking.rb:9:in `new'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/spork-0.9.0.rc8/lib/spork/run_strategy/forking.rb:9:in `run'
/Users/JP2/.rvm/gems/ruby-1.9.2-p290/gems/spork-0.9.0.rc8/lib/spork/server.rb:48:in `run'
/Users/JP2/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/drb/drb.rb:1558:in `perform_without_block'
/Users/JP2/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/drb/drb.rb:1518:in `perform'
/Users/JP2/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/drb/drb.rb:1592:in `block (2 levels) in main_loop'
/Users/JP2/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/drb/drb.rb:1588:in `loop'
/Users/JP2/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/drb/drb.rb:1588:in `block in main_loop'

我刚刚发现,如果我在没有运行Spork的情况下尝试测试,那么测试会通过,但是如果Spork正在运行,我会得到上面的错误。

这是我的spec_helper.rb文件:

    require 'rubygems'
require 'spork'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However, 
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'

  # Requires supporting files with custom matchers and macros, etc,
  # in ./support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    # == Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr
    config.mock_with :rspec

    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, comment the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true

    def test_sign_in(user)
      controller.sign_in(user)
    end
  end
end

Spork.each_run do
end

这是我的relationships_controller.rb文件:
    class RelationshipsController < ApplicationController
  before_filter :authenticate

  def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
    redirect_to @user
    end
  end

  def destroy
    @user = Relationship.find(params[:id]).followed
    current_user.unfollow!(@user)
    redirect_to @user
    end
  end
end

这是我的relationships_controller_spec.rb文件:

    require 'spec_helper'

describe RelationshipsController do

  describe "access control" do

    it "should require signin for create" do
      post :create
      response.should redirect_to(signin_path)
    end

    it "should require signin for destroy" do
      delete :destroy, :id => 1
      response.should redirect_to(signin_path)
    end
  end

  describe "POST 'create'" do

    before(:each) do
      @user = test_sign_in(Factory(:user))
      @followed = Factory(:user, :email => Factory.next(:email))
    end

    it "should create a relationship" do
      lambda do
        post :create, :relationship => { :followed_id => @followed }
        response.should be_redirect
      end.should change(Relationship, :count).by(1)
    end
  end

  describe "DELETE 'destroy'" do

    before(:each) do
      @user = test_sign_in(Factory(:user))
      @followed = Factory(:user, :email => Factory.next(:email))
      @user.follow!(@followed)
      @relationship = @user.relationships.find_by_followed_id(@followed)
    end

    it "should destroy a relationship" do
      lambda do
        delete :destroy, :id => @relationship
        response.should be_redirect
      end.should change(Relationship, :count).by(-1)
    end
  end
end

我还添加了“关注”和“取消关注”按钮,用于创建和删除关系。如上所述,如果未运行spork,则relationship_controller_spec.rb通过。但是,当我在浏览器中单击“关注”按钮时,浏览器会出现相同的“未初始化常量RelationshipsController”错误。

在你的 relationships_controller.rb 文件中,你是定义了一个 RelationshipController(如主题所述),还是一个 RelationshipsController(如溯源所示)? - iltempo
我在我的relationships_controller.rb文件中定义了一个RelationshipsController。 - jpohly2
你能否发布你的spec_helper.rb、relationships_controller.rb和relationships_controller_spec.rb文件? - nmott
1
请确认 relationships_controller.rb 文件是否在您的 app/controllers 目录中,并仔细检查拼写。这些错误有时与拼写错误的文件或文件的意外位置有关。还要检查是否有幽灵 rspec、spork 或 rails 服务器正在运行,而没有正确关闭 - 关闭所有服务器并运行 ps aus | grep rspec(然后是 spork 和 webrat)。如果仍然看到不是 grep 的进程,则需要杀死它们或重新启动计算机。 - nmott
1个回答

7

我曾经遇到过同样的问题,如果不使用Spork测试可以正常运行,但是使用Spork测试会出现错误:

NameError: 未初始化的常量.....

问题出在我的模型中一个类的文件名与模型名称并不完全匹配。你确定你的User类和Relationship类分别命名为user.rb和relationship.rb吗?


如果其他人遇到类似的命名空间问题,这里有一些建议。需要确保在 belongs_to 中添加 class_name,就像这样:belongs_to: namespace_model, class_name: 'Namespace::Model' - Vijay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接