gpt4 book ai didi

ruby-on-rails-3 - 使用 Cucumber 测试 Devise+Omniauth(Facebook)

转载 作者:行者123 更新时间:2023-12-02 19:22:00 28 4
gpt4 key购买 nike

这是我的功能:

 Scenario: Professor is not signed up and tries to sign in with Facebook
Given I do not exist as a professor
When I sign in as a professor with Facebook
Then I should see a successful sign in message
When I return to the site
Then I should be signed in as a professor

这是 When I sign in as a professor with Facebook步骤定义

When /^I sign in as a professor with Facebook$/ do
set_omniauth
visit "/professors/auth/facebook"
end

这是 set_omniauth 帮助器的定义:

def set_omniauth(opts = {})
default = {:provider => :facebook,
:uuid => "1234",
:facebook => {
:email => "foobar@example.com",
}
}

credentials = default.merge(opts)
provider = credentials[:provider]
user_hash = credentials[provider]

OmniAuth.config.test_mode = true

OmniAuth.config.mock_auth[provider] = {
'uid' => credentials[:uuid],
"extra" => {
"user_hash" => {
"email" => user_hash[:email],
}
}
}
end

所以...当我访问/professors/auth/facebook时,这个 Action 称为

def facebook
@professor = Professor.find_for_facebook_oauth(request.env["omniauth.auth"], current_professor)
if @professor.persisted?
flash[:notice] = "Welcome! You have signed up successfully."
sign_in_and_redirect @professor, :event => :authentication
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_professor_registration_url
end
end

最后,方法 find_for_facebook_oauth 定义为:

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token["extra"]["raw_info"]
if professor = self.find_by_email(data.email)
professor
else # Create a professor with a stub password.
self.create(:email => data.email, :password => Devise.friendly_token[0,20])
end
end

运行此功能时,我收到以下错误消息:

undefined method `email' for {"email"=>"foobar@example.com"}:Hash (NoMethodError)

所以,我检查了 Facebook 实际返回的内容:

#<Hashie::Mash email="myemail@gmail.com" ...

但是这是一个与普通哈希集不同的对象:

OmniAuth.config.mock_auth[provider] = {
'uid' => credentials[:uuid],
"extra" => {
"user_hash" => {
"email" => user_hash[:email],
}
}

所以,我的问题是:我该如何正确测试这个?我关注了 OmniAuth Integration Tetsing他们设置了一个哈希值,而不是哈希值。

最佳答案

您需要使用内置的 OmniAuth 方法来创建哈希来创建 Hashie 对象:

OmniAuth.config.mock_auth[provider] = OmniAuth::AuthHash.new({
'uid' => credentials[:uuid],
"extra" => {
"user_hash" => {
"email" => user_hash[:email],
}
})

关于ruby-on-rails-3 - 使用 Cucumber 测试 Devise+Omniauth(Facebook),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10964121/

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