Rails 未初始化常量RelationshipController (NameError)
我正在关注 ruby.railstutorial.org,并对我的应用程序进行了一些细微的修改。我正在尝试运行关联控制器规范,但我不断收到以下错误:
$ 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 未运行,relationships_controller_spec.rb 将通过。但是,当我单击浏览器中的后续按钮时,浏览器会给出相同的“未初始化常量RelationshipsController”错误。
I am following the ruby.railstutorial.org with some slight modifications to my app. I am trying to run the relationships_controller_spec but I keep getting the following error:
$ 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'
I just found out that if I try the test while spork is NOT running, then the test passes, but if spork is running, I get the error above.
Here is my spec_helper.rb file:
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
Here is my relationships_controller.rb file:
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
And here is my relationships_controller_spec.rb file:
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
I also added 'follow' and 'unfollow' buttons to create and delete relationships. As mentioned above, the relationships_controller_spec.rb passes if spork is not running. But, the browser gives me the same 'uninitialized constant RelationshipsController' error when I click the followed button in the browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的测试在没有 spork 的情况下运行良好,但在 spork 失败时遇到了同样的问题,给出了错误:
NameError: uninitializedconstant.....
问题是其中一个类的文件名我的型号与型号名称不完全匹配。您确定您的用户类和关系类分别命名为user.rb和relationship.rb吗?
I had the same issue with my test running fine w/o spork but failing with spork giving the error:
NameError: uninitialized constant.....
The issue was the the filename of one of the classes in my model did not exactly match the model name. Are you sure your User class and Relationship class are named user.rb and relationship.rb respectively?