Omniauth Rspec 测试问题
我按照omniauth的railscast为twitter创建身份验证( http://railscasts.com/episodes /235-omniauth-part-1?view=comments )。它在开发中工作正常,但我无法让 rspec 检测到我已经创建了身份验证。这是我在身份验证控制器中创建函数的代码片段:
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
包含在我的路线中:
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 do count本来应该改1,结果却改了0 # ./spec/controllers/authentications_controller_spec.rb:57
我已经检查了所有内容,但无法弄清楚我做错了什么。有人可以帮忙吗?
I followed the rails cast of omniauth to create authentication for twitter ( http://railscasts.com/episodes/235-omniauth-part-1?view=comments ). It works fine in development but I can't get rspec to detect that I have created the authentication. Here is my snippet for create function in my Authentication 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
included in my routes:
match '/auth/:provider/callback' => 'authentications#create'
I have the following setup in my 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"} })
Here is my rspec code that is not working:
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
error i get is:
1) AuthenticationsController Post 'create' success should create authentication
Failure/Error: lambda do
count should have been changed by 1, but was changed by 0
# ./spec/controllers/authentications_controller_spec.rb:57
I've checked everything and cannot figure what I am doing wrong. Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于明白出了什么问题。在我的模拟中:auth_token 应该是:token。这导致了验证失败。
i finally figured what was wrong. in my mock :auth_token is suppose to be :token. That was causing the failed validation.
这不应该是一个get请求吗?
Shouldn't that be a get request?