为什么我的 RSpec 没有加载 Devise::Test::ControllerHelpers?
Why is my RSpec not loading Devise::Test::ControllerHelpers?
我正在使用 Rails 5 和 Devise 3.5.1。
阅读一本关于 creating/testing 和 API 的好(旧)书,它使用 Devise 身份验证。写在Rails5之前,所以选择不用新的api-only版本。
这是我的测试...
#/spec/controllers/api/v1/users_controller_spec.rb
require 'rails_helper'
describe Api::V1::UsersController, :type => :controller do
before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }
describe "GET #show" do
before(:each) do
@user = FactoryGirl.create :user
get :show, params: {id: @user.id}, format: :json
end
it "returns the information about a reporter on a hash" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:email]).to eql @user.email
end
it { should respond_with 200 }
end
end
这是一个完全出乎意料的 RSpec 错误
Devise::MissingWarden:
Devise could not find the `Warden::Proxy` instance on your request environment.
Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.
If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the `Devise::Test::ControllerHelpers` module to inject the `request.env['warden']` object for you.
所以我去这里 - http://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers
并尝试了这个 -> include Devise::Test::ControllerHelpers
这没有帮助,因为文件 controller_helpers.rb
在我的项目中不存在
我错过了什么?
谢谢
您可以将以下内容添加到您的rails_helper
:
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
end
这将在所有 :controller
规范中包含 Devise::Test::ControllerHelpers
模块。
在spec_helper.rb
中添加:
config.include Devise::Test::ControllerHelpers, :type => :controller
迷你测试,Rails 4
这适用于原版 Rails 4 MiniTest
test\controllers\users_controller_test.rb
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
include Warden::Test::Helpers
include Devise::Test::ControllerHelpers
setup do
# https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
# @user = users(:admin)
# sign_in @user
end
teardown do
Warden.test_reset!
end
test "login as admin" do
@user = users :admin
sign_in @user
get :dashboard
assert_redirected_to admin_dashboard_path
end
end
我正在使用 Rails 5 和 Devise 3.5.1。
阅读一本关于 creating/testing 和 API 的好(旧)书,它使用 Devise 身份验证。写在Rails5之前,所以选择不用新的api-only版本。
这是我的测试...
#/spec/controllers/api/v1/users_controller_spec.rb
require 'rails_helper'
describe Api::V1::UsersController, :type => :controller do
before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }
describe "GET #show" do
before(:each) do
@user = FactoryGirl.create :user
get :show, params: {id: @user.id}, format: :json
end
it "returns the information about a reporter on a hash" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:email]).to eql @user.email
end
it { should respond_with 200 }
end
end
这是一个完全出乎意料的 RSpec 错误
Devise::MissingWarden:
Devise could not find the `Warden::Proxy` instance on your request environment.
Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.
If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the `Devise::Test::ControllerHelpers` module to inject the `request.env['warden']` object for you.
所以我去这里 - http://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers
并尝试了这个 -> include Devise::Test::ControllerHelpers
这没有帮助,因为文件 controller_helpers.rb
在我的项目中不存在
我错过了什么?
谢谢
您可以将以下内容添加到您的rails_helper
:
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
end
这将在所有 :controller
规范中包含 Devise::Test::ControllerHelpers
模块。
在spec_helper.rb
中添加:
config.include Devise::Test::ControllerHelpers, :type => :controller
迷你测试,Rails 4
这适用于原版 Rails 4 MiniTest
test\controllers\users_controller_test.rb
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
include Warden::Test::Helpers
include Devise::Test::ControllerHelpers
setup do
# https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
# @user = users(:admin)
# sign_in @user
end
teardown do
Warden.test_reset!
end
test "login as admin" do
@user = users :admin
sign_in @user
get :dashboard
assert_redirected_to admin_dashboard_path
end
end