gpt4 book ai didi

ruby-on-rails - 您如何使用 rspec 测试 omni-auth facebook?

转载 作者:行者123 更新时间:2023-12-04 20:17:28 26 4
gpt4 key购买 nike

我查看了 gem wiki 并按照说明操作,但由于某种原因,我收到了 nil在进行 omniauth 测试时:

user_sessions_controller_spec.rb:

require 'spec_helper'

describe UserSessionsController, "OmniAuth" do
before do
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
end

it "sets a session variable to the OmniAuth auth hash" do
request.env["omniauth.auth"]['uid'].should == '123545'
end
end

spec_helper.rb:
RACK_ENV = ENV['ENVIRONMENT'] ||= 'test'
OmniAuth.config.test_mode = true
omniauth_hash =
{:provider => "facebook",
:uid => "1234",
:info => {:name => "John Doe",
:email => "johndoe@email.com"},
:credentials => {:token => "testtoken234tsdf"}}

OmniAuth.config.add_mock(:facebook, omniauth_hash)

规范结果:
Failures:

1) UserSessionsController OmniAuth sets a session variable to the OmniAuth auth hash
Failure/Error: request.env["omniauth.auth"]['uid'].should == '123545'
NoMethodError:
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.[]
# ./spec/controllers/user_sessions_controller_spec.rb:10:in `block (2 levels) in <top (required)>'

最佳答案

在测试中尝试使用符号而不是字符串:

  it "sets a session variable to the OmniAuth auth hash" do
request.env["omniauth.auth"][:uid].should == '1234'
end

我不确定 Omniauth 是否在某个时候从字符串更改为符号,但似乎有许多使用字符串作为键的示例。

为了完整起见,如果有人尝试使用 Devise 和 Omniauth 执行此操作,请不要忘记在 before 块中添加 devise.mapping。
before do
request.env["devise.mapping"] = Devise.mappings[:user]
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
end

关于ruby-on-rails - 您如何使用 rspec 测试 omni-auth facebook?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467226/

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