如何使用Minitest测试OmniAuth的SessionsController
我正在尝试使用Minitest测试我的Facebook登录代码,但它并不顺利。
当我尝试get :auth, 'provider' => 'facebook'
在测试中,测试返回undefined method '[]' for nil:NilClass
,如果我尝试get :new
它传递,但auth不被调用。
如何使用omniauth成功重定向到auth?
这是我的源代码。
测试/控制器/ sessions_controller_test.rb
require 'test_helper' class SessionsControllerTest 'facebook', 'uid' => '123451234512345', 'info' => {'email' => 'testuser@testmail.com', 'name' => 'test', 'image' => ''} }) request.env['omniauth.env'] = OmniAuth.config.mock_auth[:facebook] get :auth end end
应用程序/控制器/ sessions_controller.rb
class SessionsController < ApplicationController def auth if(env['omniauth.params']['type'] == 'signin') if User.find_by(uid: env['omniauth.auth'].uid) flash[:alert] = "Already registered" redirect_to :back else user = User.omniauth(env['omniauth.auth']) if user.save session[:user_id] = user.uid redirect_to root_url else flash[:alert]="Failed to Signin" redirect_to :back end end elsif(env['omniauth.params']['type'] == 'login') if User.find_by(uid: env['omniauth.auth'].uid) session[:user_id] = env['omniauth.auth'].uid redirect_to root_url else flash[:alert] = "Not registered" redirect_to :back end else redirect_to root_url end end def find redirect_to '/auth/facebook?type=login' end def new redirect_to '/auth/facebook?type=signin' end def destroy session[:user_id] = nil redirect_to root_url end end
我想你可能只需要在SessionsController#auth
使用request.env[...]
而不是env[...]
。