在请求规范中存根身份验证
在编写请求规范时,如何设置会话和/或存根控制器方法? 我正在尝试在集成测试中消除身份验证 - rspec/requests
这是一个测试示例
require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/authentication_helpers'
describe "Messages" do
include AuthenticationHelpers
describe "GET admin/messages" do
before(:each) do
@current_user = Factory :super_admin
login(@current_user)
end
it "displays received messages" do
sender = Factory :jonas
direct_message = Message.new(:sender_id => sender.id, :subject => "Message system.", :content => "content", :receiver_ids => [@current_user.id])
direct_message.save
get admin_messages_path
response.body.should include(direct_message.subject)
end
end
end
助手:
module AuthenticationHelpers
def login(user)
session[:user_id] = user.id # session is nil
#controller.stub!(:current_user).and_return(user) # controller is nil
end
end
以及处理身份验证的 ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
helper_method :logged_in?
protected
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!current_user.nil?
end
end
为什么无法访问这些资源?
1) Messages GET admin/messages displays received messages
Failure/Error: login(@current_user)
NoMethodError:
undefined method `session' for nil:NilClass
# ./spec/requests/authentication_helpers.rb:3:in `login'
# ./spec/requests/message_spec.rb:15:in `block (3 levels) in <top (required)>'
When writing a request spec, how do you set sessions and/or stub controller methods?
I'm trying to stub out authentication in my integration tests - rspec/requests
Here's an example of a test
require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/authentication_helpers'
describe "Messages" do
include AuthenticationHelpers
describe "GET admin/messages" do
before(:each) do
@current_user = Factory :super_admin
login(@current_user)
end
it "displays received messages" do
sender = Factory :jonas
direct_message = Message.new(:sender_id => sender.id, :subject => "Message system.", :content => "content", :receiver_ids => [@current_user.id])
direct_message.save
get admin_messages_path
response.body.should include(direct_message.subject)
end
end
end
The helper:
module AuthenticationHelpers
def login(user)
session[:user_id] = user.id # session is nil
#controller.stub!(:current_user).and_return(user) # controller is nil
end
end
And the ApplicationController that handles authentication:
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
helper_method :logged_in?
protected
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!current_user.nil?
end
end
Why is it not possible to access these resources?
1) Messages GET admin/messages displays received messages
Failure/Error: login(@current_user)
NoMethodError:
undefined method `session' for nil:NilClass
# ./spec/requests/authentication_helpers.rb:3:in `login'
# ./spec/requests/message_spec.rb:15:in `block (3 levels) in <top (required)>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请求规范是
ActionDispatch::IntegrationTest
的薄包装,它不像控制器规范那样工作(它包装ActionController::TestCase
)。尽管有可用的会话方法,但我认为它不受支持(即它可能存在,因为其他实用程序包含的模块也包含该方法)。我建议通过发布到您用来验证用户身份的任何操作来登录。如果您为所有用户工厂设置密码“password”(例如),那么您可以执行以下操作:
A request spec is a thin wrapper around
ActionDispatch::IntegrationTest
, which doesn't work like controller specs (which wrapActionController::TestCase
). Even though there is a session method available, I don't think it is supported (i.e. it's probably there because a module that gets included for other utilities also includes that method).I'd recommend logging in by posting to whatever action you use to authenticate users. If you make the password 'password' (for example) for all the User factories, then you can do something like this:
Devise 用户请注意...
顺便说一句,如果您使用 Devise。我在集成/请求测试中所做的事情(感谢 这篇 StackOverflow 帖子):
Note for Devise users...
BTW, @David Chelimsky's answer may need a little tweaking if you're using Devise. What I'm doing in my integration / requests testing (thanks to this StackOverflow post):
FWIW,在将我的 Test::Unit 测试移植到 RSpec 时,我希望能够在我的请求规范中使用多个(设计)会话登录。需要一些挖掘,但让它对我有用。使用 Rails 3.2.13 和 RSpec 2.13.0。
并且...
编辑:修复了拼写错误
FWIW, in porting my Test::Unit tests to RSpec, I wanted to be able to login with multiple (devise) sessions in my request specs. It took some digging, but got this to work for me. Using Rails 3.2.13 and RSpec 2.13.0.
And...
Edit: fixed typo
您也可以很容易地对会话进行存根。
所有 ruby 特殊运算符确实都是方法。调用
1+1
与1.+(1)
相同,这意味着+
只是一个方法。同样,session[:user_id]
与在session
上调用方法[]
相同,如session.[](:user_id )
You could pretty easily stub the session as well.
All ruby special operators are indeed methods. Calling
1+1
is the same as1.+(1)
, which means+
is just a method. Similarly,session[:user_id]
is the same as calling method[]
onsession
, assession.[](:user_id)
我发现这对 Devise 非常有帮助: https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)
I found this very helpful for Devise : https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)