2

コントローラーをテストするための仕様(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
4

1 に答える 1

7

バックトレースは、セットアップ コールバックの順序でこの問題rspec-rails 2.10.0が発生した使用しているように見えます。その特定のgemのバージョンを修正する必要があるため、アップグレードしてみてください。2.10.1

あなたは正しい道を進んでいます

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
  #...
end

include Devise::TestHelpersしたがって、spec ファイルから明示的に削除できると思います。

于 2012-09-03T19:46:16.323 に答える