Ruby on rails omniauth模拟facebook响应
我想通过facebook测试我的登录。我使用的是纯omniauth,没有设计。我检查wiki页面并执行以下操作: 请求规格的助手Ruby on rails omniauth模拟facebook响应,ruby-on-rails,facebook,rspec,mocking,omniauth,Ruby On Rails,Facebook,Rspec,Mocking,Omniauth,我想通过facebook测试我的登录。我使用的是纯omniauth,没有设计。我检查wiki页面并执行以下操作: 请求规格的助手 module IntegrationSpecHelper def login_with_oauth(service = :facebook) visit "/auth/#{service}" end end spec_helper.rb RSpec.configure do |config| config.include Inte
module IntegrationSpecHelper
def login_with_oauth(service = :facebook)
visit "/auth/#{service}"
end
end
spec_helper.rb
RSpec.configure do |config|
config.include IntegrationSpecHelper, :type => :request
end
Capybara.default_host = 'http://example.org'
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:facebook, {
:provider => 'facebook',
:uid => '12345',
:user_info => {
:name => 'dpsk'
}
})
我的规格
require 'spec_helper'
describe 'facebook' do
it "should login with facebook", :js => true do
login_with_oauth
visit '/'
page.should have_content("dpsk")
end
end
#OmniAuth routes
match "/auth/:provider/callback" => "sessions#create"
match "/signout" => "sessions#destroy", :as => :signout
match "/signin" => "sessions#signin", :as => :signin
match "/auth/failure" => "sessions#failure", :as => :auth_failure
但在规范中,没有返回任何内容,而不是我的模拟,我得到了一个错误:
Failure/Error: visit "/auth/facebook"
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]
我的错误在哪里?我的问题在模拟中,它的结构错误
OmniAuth.config.mock_auth[:facebook] = {
'user_info' => {
'name' => 'Mario Brothers',
'image' => '',
'email' => 'dpsk@email.ru' },
'uid' => '123545',
'provider' => 'facebook',
'credentials' => {'token' => 'token'}
}
该结构对我有效(2016年12月)
若我用mock和host注释掉了部分(左侧仅启用测试模式=true),那个么行为并没有改变。omniauth似乎没有看到我的模拟:|会话控制器和用户模型是什么样子的?可能值得注意的是,这应该在AuthHash结构中。只需初始化
OmniAuth::AuthHash.new({您拥有的HASH})
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
'provider' => 'facebook',
'uid' => '123545',
'info' => {
'email' => 'dpsk@email.ru',
'name' => 'Mario Brothers',
'image' => '' },
'credentials'=> {
'token'=> '12345',
'expires_at' => 1486718672,
'expires' => true },
'extra' => {
'raw_info' => {
'email' => 'dpsk@email.ru',
'name' => 'Mario Brothers',
'id' => '12345' }
}
})