Ruby-on-rails 如何在 rspec 功能测试中访问(设计)current_user?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23793597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:38:59  来源:igfitidea点击:

How to access (devise) current_user in a rspec feature test?

ruby-on-railsrubyrspecdevise

提问by Hommer Smith

In the devise documentation they give tips on how you can have access to current_user when testing a controller:

在设计文档中,他们提供了有关在测试控制器时如何访问 current_user 的提示:

https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29

https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29

However, what about when doing a feature test? I am trying to test a create method of one of my controllers, and in that controller is used the current_user variable.

但是,在进行功能测试时呢?我正在尝试测试我的一个控制器的 create 方法,并且在该控制器中使用了 current_user 变量。

The problem is that the macro suggested in devise uses the @request variable, and it is nil for a feature spec. What is a workaround?

问题在于 devise 中建议的宏使用了 @request 变量,而对于功能规范,它为零。什么是解决方法?

EDIT:

编辑:

This is what I have so far for my current spec:

这是我目前的规格:

feature 'As a user I manage the orders of the system' do
  scenario 'User is logged in ad an admin' do
    user = create(:user)
    order = create(:order, user: user)
    visit orders_path
    #Expectations
  end
end

The problem is that in my OrdersControllerI have a current_user.orderscall, and since current_useris not defined, it will redirect me to /users/sign_in.

问题是OrdersController我有一个current_user.orders电话,由于current_user没有定义,它会将我重定向到/users/sign_in.

I have defined this under /spec/features/manage_orders.rb

我已经定义了这个 /spec/features/manage_orders.rb

回答by RMazitov

from https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29

来自https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29

if i have understood you right, maybe you need to use

如果我理解正确,也许你需要使用

subject.current_user.email
#or
controller.current_user.email

for example :

例如 :

describe OrdersController, :type => :controller do
  login_user

  describe "POST 'create'" do
     it "with valid parametres" do
        post 'create', title: 'example order', email: subject.current_user.email
     end
  end
end

controller_macros.rb :

控制器宏.rb :

module ControllerMacros
  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = FactoryGirl.create(:user)
      #user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module
      sign_in user
    end
  end
end

Don't forget to include this into your spec_helper.rb :

不要忘记将其包含在您的 spec_helper.rb 中:

config.include Devise::TestHelpers, type: :controller
config.extend ControllerMacros, type: :controller

回答by Alec Rooney

Here's what I think you are looking for:

这是我认为您正在寻找的内容:

require 'spec_helper'
include Warden::Test::Helpers
Warden.test_mode!

feature 'As a user I manage the orders of the system' do
  scenario 'User is logged in ad an admin' do
    user = create(:user)
    login_as(user, scope: :user)
    order = create(:order, user: user)
    visit orders_path
    #Expectations
  end
end

回答by Hany Elsioufy

you can define login_user as a method for the user to login as follows (put it in support folder):

您可以将 login_user 定义为用户登录的方法,如下所示(将其放在支持文件夹中):

def login_user
  Warden.test_mode!
  user = create(:user)
  login_as user, :scope => :user
  user.confirmed_at = Time.now
  user.confirm!
  user.save
  user
end

Then in the scenario say:

然后在场景中说:

user = login_user