且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

rspec不能使用设计用户身份验证

更新时间:2023-12-06 09:35:40

似乎需要 require'support / controller_macros' spec / support 子目录中的文件是自动加载但不是必需的。


I am attempting to use RSpec to test my rails application which uses devise user authentication gem.

Here are a list of relevant gems already bundled:

devise (3.4.1)
rails (4.1.7, 4.1.6)
rspec (3.1.0)
rspec-core (3.1.7)
rspec-expectations (3.1.2)
rspec-mocks (3.1.3)
rspec-rails (3.1.0)
rspec-support (3.1.2)

As per the documentation for devise and rspec I have a spec/controller/feed_controller_spec.rb like this:

require 'rails_helper'

RSpec.describe FeedController, :type => :controller do

  before(:each) do
    devise_setup
  end

  it "blocks unauthenticated access" do
    get :index
    response.should redirect_to(new_user_session_path)
  end

  it "allows authenticated access" do
    user = User.create!(:foo => 'bar', ...)
    sign_in user
    get :index
    expect(response).to be_success
  end
end

And in my spec/support/controller_macros.rb file:

module ControllerMacros
  def devise_setup
      allow(request.env['warden']).to receive(:authenticate!).and_throw(:warden, {:scope => :user})
      allow(controller).to receive(:current_user).and_return(nil)
  end

  def sign_in(user)
    if user.nil?
      allow(request.env['warden']).to receive(:authenticate!).and_throw(:warden, {:scope => :user})
      allow(controller).to receive(:current_user).and_return(nil)
    else
      allow(request.env['warden']).to receive(:authenticate!).and_return(user)
      allow(controller).to receive(:current_user).and_return(user)
    end
  end
end

And last but not least my spec/spec_helper.rb file has the following added lines (at the top of the file that was created using rails generate rspec:init command):

require 'devise'

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

When I run bundle exec rspec on the command line I receive the following error:

/.../spec/spec_helper.rb:6:in `block in <top (required)>': uninitialized constant ControllerMacros (NameError)
    from /usr/local/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core.rb:81:in `configure'
    from /home/vagrant/code/pulse/spec/spec_helper.rb:4:in `<top (required)>'

When I comment out the ControlMacros line from the spec_helper.rb file it gives the following:

1) FeedController blocks unauthenticated access
   Failure/Error: devise_setup
   NameError:
     undefined local variable or method `devise_setup' for #<RSpec::ExampleGroups::FeedController:0xba5d20f4>
   # ./spec/controllers/feed_controller_spec.rb:9:in `block (2 levels) in <top (required)>'

2) FeedController allows authenticated access
   Failure/Error: devise_setup
   NameError:
     undefined local variable or method `devise_setup' for #<RSpec::ExampleGroups::FeedController:0xba5c76a4>
   # ./spec/controllers/feed_controller_spec.rb:9:in `block (2 levels) in <top (required)>'

Seems like you need to require 'support/controller_macros'. Files in the spec/support subdirectory are auto-loaded but not required.