我的Rails 3.2应用程序使用OmniAuth和Devise来登录Twitter.身份验证系统工作正常.我想在rspec中编写一个集成测试,以确保一切正常.使用wiki中的信息,我写了以下内容,但我知道我错过了一些东西.
在config/environments中的test.rb下,我有以下几行
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:twitter] = {:provider => 'twitter', :uid => '123545'}
Run Code Online (Sandbox Code Playgroud)
我的rspec测试看起来像这样:
describe "Authentications" do
context "without signing into app" do
it "twitter sign in button should lead to twitter authentication page" do
visit root_path
click_link "Sign in with Twitter"
Authentication.last.uid.should == '123545'
end
end
end
Run Code Online (Sandbox Code Playgroud)
身份验证是我的模型的名称,在rails控制台中调用.uid会返回字符串.
运行此测试时出现以下错误:
Failure/Error: Authentication.last.uid.should == '123545'
NoMethodError:
undefined method `uid' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我弄清楚如何使用提供的OmniAuth模拟?关于它为什么以及如何工作的解释也将受到赞赏.
integration-testing rspec rspec-rails omniauth ruby-on-rails-3
在我们的rails 3.1.4 app中,rspec用于测试require_signin应用程序控制器中的公共方法.这是方法require_signin:
def require_signin
if !signed_in?
flash.now.alert = "Log in first!"
redirect_to signin_path
end
end
Run Code Online (Sandbox Code Playgroud)
这是rspec代码:
it "should invoke require_signin for those without login" do
controller.send(:require_signin)
controller {should redirect_to signin_path}
end
Run Code Online (Sandbox Code Playgroud)
上面会rspec生成巨大的多页错误,如下所示:
RuntimeError:?[0m
?[31mActionController::RackDelegation#status= delegated to @_response.status=, but @_response is nil: #<ApplicationController:0x3
a67f10 @_routes=nil, @_action_has_layout=true, @_view_context_class=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_reques
t=#<ActionController::TestRequest:0x3a68720 @env={"rack.version"=>[1, 1], "rack.input"=>#<StringIO:0x34fad60>, ........
Run Code Online (Sandbox Code Playgroud)
可能是什么错误的rspec代码?非常感谢.
我有一个不能被直接访问,在传统的RESTful方式的控制,而只能通过特定的URL.
通常我已经习惯了使用GET和POST在我的控制器的规格调用控制器动作.有没有办法通过访问特定网址来运用我的控制器?
编辑:
这是我的路线:
Larzworld::Application.routes.draw do
match '/auth/:provider/callback' => 'authentications#create'
devise_for :users, :controllers => {:registrations => "registrations"}
root :to => 'pages#home'
end
Run Code Online (Sandbox Code Playgroud)
这是我的规格:
require 'spec_helper'
describe AuthenticationsController do
before(:each) do
request.env["omniauth.auth"] = {"provider" => "twitter", "uid" => "12345678"}
end
describe 'POST create' do
it "should find the Authentication using the uid and provider from omniauth" do
Authentication.should_receive(:find_by_provider_and_uid)
post 'auth/twitter/callback'
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误:
Failures:
1) AuthenticationsController POST create should find the Authentication using the uid and provider from omniauth
Failure/Error: …Run Code Online (Sandbox Code Playgroud)