gpt4 book ai didi

ruby-on-rails - Omniauth Rspec 测试问题

转载 作者:行者123 更新时间:2023-12-04 02:25:27 25 4
gpt4 key购买 nike

我遵循 omniauth 的 rails cast 为 twitter ( http://railscasts.com/episodes/235-omniauth-part-1?view=comments ) 创建身份验证。它在开发中运行良好,但我无法让 rspec 检测到我已经创建了身份验证。这是我的身份验证 Controller 中创建函数的片段:

def create
begin

auth_hash = request.env["omniauth.auth"]
@auth = current_user.authentications.build( :provider => auth_hash['provider'],
:uid => auth_hash['uid'],
:name => auth_hash['user_info']['name'],
:nickname => auth_hash['user_info']['nickname'],
:image => auth_hash['user_info']['image']
)

if @auth.provider.downcase == "twitter"
@auth.auth_token = auth_hash['credentials']['token']
@auth.secret_token = auth_hash['credentials']['secret']
@auth.site = auth_hash['user_info']['urls']['Twitter']
elsif @auth.provider == "Facebook"

end

rescue
redirect_to current_user, :flash => { :error => "Missing oauth data!! Check with administrator!"}
else
if @auth.save
msg = "Authentication success"
else
msg = "Already have authentication"
end
redirect_to current_user, :notice => msg
end
end

包含在我的 route :
match '/auth/:provider/callback' => 'authentications#create'

我的 rspec_helper 中有以下设置:
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:twitter, { :provider => "twitter",
:uid => "1234",
:user_info => { :name => "Bob hope",
:nickname => "bobby",
:urls => {:Twitter => "www.twitter.com/bobster"}},
:credentials => { :auth_token => "lk2j3lkjasldkjflk3ljsdf"} })

这是我的 rspec 代码不起作用:
  describe "Post 'create'" do
before(:each) do
@user = Factory(:user)
sign_in @user
end

describe "success" do
before(:each) do
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:twitter]
end


it "should create authentication" do
lambda do
post :create, :provider => "twitter"
response.should redirect_to(@user)
end.should change(Authentication, :count).by(1)
end
end
end

我得到的错误是:

1) AuthenticationsController Post 'create' 成功应该创建身份验证
失败/错误:lambda 做
计数本应更改为 1,但已更改为 0
# ./spec/controllers/authentications_controller_spec.rb:57

我已经检查了所有内容,但无法弄清楚我做错了什么。任何人都可以帮忙吗?

最佳答案

我终于明白出了什么问题。在我的模拟中:auth_token 应该是:token。这导致验证失败。

关于ruby-on-rails - Omniauth Rspec 测试问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7265414/

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