Ruby on rails 如何使用Omniauth gem登录用户进行Rails测试?
我已经向gem提出了申请。我的所有rails测试都通过了,但在添加代码以检查经过身份验证的用户是否可以访问某些操作之后,我的一些测试失败了。如何向测试中添加代码以登录“测试”用户,从而模拟经过身份验证的用户?大多数google结果都显示了Rspec或Desive的示例,但我一直在使用Rails 5的默认测试套件,因此我很难找到一些如何正确实现这一点的示例。如果您想让我提供我当前测试的任何具体样本,请告诉我。因为我不知道从哪里开始,所以我没有想到要包括任何相关内容。所以在阅读了文档并尝试了各种方法之后,我想到了以下内容 测试/测试助手.rbRuby on rails 如何使用Omniauth gem登录用户进行Rails测试?,ruby-on-rails,authentication,ruby-on-rails-5,minitest,omniauth-facebook,Ruby On Rails,Authentication,Ruby On Rails 5,Minitest,Omniauth Facebook,我已经向gem提出了申请。我的所有rails测试都通过了,但在添加代码以检查经过身份验证的用户是否可以访问某些操作之后,我的一些测试失败了。如何向测试中添加代码以登录“测试”用户,从而模拟经过身份验证的用户?大多数google结果都显示了Rspec或Desive的示例,但我一直在使用Rails 5的默认测试套件,因此我很难找到一些如何正确实现这一点的示例。如果您想让我提供我当前测试的任何具体样本,请告诉我。因为我不知道从哪里开始,所以我没有想到要包括任何相关内容。所以在阅读了文档并尝试了各种方法
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:facebook, {:uid => '123456', info: { email: 'email_address_of_user_in_fixtures@gmail.com' }})
def sign_in_admin
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
get auth_facebook_callback_path
end
end
require 'test_helper'
class RestaurantsControllerTest < ActionDispatch::IntegrationTest
setup do
# I sign in an admin user, and proceed
sign_in_admin
@restaurant = restaurants(:mcdonalds)
end
# other test methods for actions ...
end
Rails.application.routes.draw do
#...
get 'auth/facebook/callback', to: 'sessions#create'
#...
end
测试/控制器/餐厅控制器\u测试.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:facebook, {:uid => '123456', info: { email: 'email_address_of_user_in_fixtures@gmail.com' }})
def sign_in_admin
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
get auth_facebook_callback_path
end
end
require 'test_helper'
class RestaurantsControllerTest < ActionDispatch::IntegrationTest
setup do
# I sign in an admin user, and proceed
sign_in_admin
@restaurant = restaurants(:mcdonalds)
end
# other test methods for actions ...
end
Rails.application.routes.draw do
#...
get 'auth/facebook/callback', to: 'sessions#create'
#...
end
您可以只存根检查已验证用户的方法