ruby-on-rails - 在 rspec 特性测试中使用 Devise

标签 ruby-on-rails testing rspec devise

我编写了以下 rspec 功能测试规范:

require "rails_helper"

RSpec.describe "Team management", type: :feature do
  user_sign_in

  describe "User creates a new team" do
    ...    
    expect(page).to have_link("#{team_name}")
  end
end

user_sign_in 方法在我的 rails_helper.rb 中定义:

ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'capybara/rails'

...

module UserSignInHelpers
  def user_sign_in
    before(:each) do
      @request.env['devise.mapping'] = Devise.mappings[:user]

      @current_user = FactoryGirl.create(:user)
      @current_user.confirm

      sign_in :user, @current_user
    end
  end
end

RSpec.configure do |config|
  ...
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  config.extend UserSignInHelpers,    type: :controller
  config.extend UserSignInHelpers,    type: :feature
  config.include Devise::TestHelpers, type: :controller
  config.include Devise::TestHelpers, type: :feature
end

user_sign_in 方法适用于我所有的 Controller 规范,但是当我运行我的功能规范时它失败了:

Team management
  User creates a new team
    example at ./spec/features/user_creates_a_new_team_spec.rb:19 (FAILED - 1)

Failures:

  1) Team management User creates a new team
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `env' for nil:NilClass
     # /Users/xxxx/.rvm/gems/ruby-2.2.1/gems/devise-3.5.1/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'

我不明白为什么这适用于 Controller 测试而不适用于功能测试。有什么我可以做的让这个在功能测试中工作吗?

最佳答案

There is a basic problem with what you are trying to do. (Devise work on top of Warden)Warden is a Rack middleware, but RSpec controller specs don't even include Rack, as these types of specs are not meant to run your full application stack, but only your controller code.

Ref

Test with Capybara

我有一个简单的支持助手,它允许我登录和注销用户:

module Auth
  def create_user!
    @user = User.create(email: 'foo@bar.com', password: '11111111')
  end

  def sign_in_user!
    setup_devise_mapping!
    sign_in @user
  end

  def sign_out_user!
    setup_devise_mapping!
    sign_out @user
  end

  def setup_devise_mapping!
    @request.env["devise.mapping"] = Devise.mappings[:user]
  end

  def login_with_warden!
    login_as(@user, scope: :user)
  end

  def logout_with_warden!
    logout(:user)
  end

  def login_and_logout_with_devise
    sign_in_user!
    yield
    sign_out_user!
  end

  def login_and_logout_with_warden
    Warden.test_mode!
    login_with_warden!
    yield
    logout_with_warden!
    Warden.test_reset!
  end
end

在功能中:

RSpec.describe "Team management", type: :feature do
  describe "User creates a new team" do
     login_and_logout_with_warden do
       # tests goes here
     end
  end
end

在 Controller 中:

RSpec.describe "Team management", type: :controller do
  describe "User creates a new team" do
    login_and_logout_with_devise do
      # tests goes here
    end
  end
end

关于ruby-on-rails - 在 rspec 特性测试中使用 Devise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32628093/

相关文章:

testing - 在哪里可以获得样本 3d 环境?

ruby-on-rails - 如何使用 rspec 和 capybara 测试索引/确认/完成流页面?

ruby-on-rails - Windows 上的 Ruby/Rails 前纪元日期

ruby-on-rails - 将 carrierwave 与 Bearer token 授权一起使用

ruby-on-rails - 使用 PostgreSQL 的模式和 Rails 创建 Multi-Tenancy 应用程序

ruby - RSpec - 通用失败消息而不是有用的输出

ruby-on-rails - Elasticsearch + 轮胎 : good strategy to mock ES

ruby-on-rails - Rails 加载页面的时间比预期的要长

asp.net - 尝试模拟依赖项并向 IoC 容器注册时出现 System.InvalidOperationException

ruby-on-rails - 为什么 Rspec No route matches error with Rails?