gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:59:12 27 4
gpt4 key购买 nike

我编写了以下 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com