我有这样的规范(spec/controllers/stats_controller_spec.rb)来测试我的控制器:
require 'spec_helper'
describe StatsController do
render_views
before(:each){ @user = FactoryGirl.create( :user ) }
describe "GET 'users_in_system'" do
describe "for non-signed users" do
it "should deny access" do
get :users_in_system, :locale => :en
#...
end
end
end
end
当我运行这个规范时,我得到这样的错误:
1) StatsController GET 'users_in_system' for non-signed users should deny access
Failure/Error: get :users_in_system, :locale => :en
ActionView::Template::Error:
undefined method `authenticate' for nil:NilClass
# ./app/views/layouts/_navbar_template.html.haml:16:in `_app_views_layouts__navbar_template_html_haml__2938837959272005060_70149962955560'
# ./app/views/layouts/application.html.haml:12:in `_app_views_layouts_application_html_haml__243298846079092947_70149956982200'
# ./spec/controllers/stats_controller_spec.rb:12:in `block (4 levels) in <top (required)>'
我试图通过将此行添加到 (spec/spec_helper.rb) 来解决此错误:
#...
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
#...
end
然后我开始收到这样的错误:
1) StatsController GET 'users_in_system' for non-signed users should deny access
Failure/Error: Unable to find matching line from backtrace
NoMethodError:
undefined method `env' for nil:NilClass
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/devise-2.1.0/lib/devise/test_helpers.rb:33:in `warden'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-rails-2.10.0/lib/rspec/rails/adapters.rb:15:in `block (2 levels) in setup'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example.rb:178:in `instance_eval'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example.rb:178:in `instance_eval'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/hooks.rb:23:in `run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/hooks.rb:63:in `block in run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/hooks.rb:63:in `each'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/hooks.rb:63:in `run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/hooks.rb:400:in `run_hook'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:298:in `run_before_each_hooks'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example.rb:239:in `run_before_each'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example.rb:86:in `block in run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example.rb:195:in `with_around_each_hooks'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example.rb:84:in `run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:353:in `block in run_examples'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:349:in `map'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:349:in `run_examples'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:335:in `run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:336:in `block in run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:336:in `map'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:336:in `run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:336:in `block in run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:336:in `map'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/example_group.rb:336:in `run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:28:in `map'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:28:in `block in run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/reporter.rb:34:in `report'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:25:in `run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:69:in `run'
# /Users/serg/.rvm/gems/ruby-1.9.3-head/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'
我尝试添加(spec/controllers/stats_controller_spec.rb):
describe StatsController do
render_views
include Devise::TestHelpers
#...
end
但这并没有改变。我该如何修复它?
UPD:我的统计控制器:
class StatsController < ApplicationController
def users_in_system
@users = User.all
end
end
我的导航栏:
%div{ :class => "navbar navbar-inverse navbar-fixed-top" }
%div{ :class => "navbar-inner" }
%a{:class => "btn btn-navbar", "data-toggle" => "collapse", "data-target" => ".nav-collapse"}
%span{:class=>"icon-bar"}
%span{:class=>"icon-bar"}
%span{:class=>"icon-bar"}
= link_to t(".brand_name"), root_path, { :class => "brand" }
%div{ :class => "nav-collapse" }
-# Left navigation.
%ul{ :class => "nav" }
%li=link_to "About", about_path
-# Right navigation.
%ul{ :class => "nav pull-right" }
- if user_signed_in?
%li=link_to t(".settings"), edit_user_registration_path
%li{ :class => "divider-vertical" }
%li=link_to t(".logout_link"), destroy_user_session_path, :method => :delete