使用 Rspec、NiftyAuthentication 进行测试
我正在使用 Ryan Bates 的 nifty:authentication
,并开始使用 Rspec 进行测试。与这个问题斗争了几个星期,但仍然不明白发生了什么。
我的控制器只是调用
before_filter :login_required, :except => [:login]
lib/controller_authentication 中定义的
def self.included(controller)
controller.send :helper_method, :current_account, :logged_in?, :redirect_to_target_or_default
end
def current_account
@current_account ||= Account.find(session[:account_id]) if session[:account_id]
end
def logged_in?
current_account
end
def login_required
unless logged_in?
store_target_location
redirect_to login_url, :alert => "You must first answer me these riddles three log in or sign up before accessing this page."
end
end
def redirect_to_target_or_default(default, *args)
redirect_to(session[:return_to] || default, *args)
session[:return_to] = nil
end
private
def store_target_location
session[:return_to] = request.url
end
end
应用程序按预期工作,但测试每次都失败。无论我尝试什么,我都会得到 redirect_to login_url, :alert =>; “您必须首先...登录”
页面。
我尝试过的事情:
controller.stub!( :login_required )
ControllerAuthentication.stub!(:current_account).and_return(Account.where(:username => 'ej0c').first)
#ControllerAuthentication.stub!(:logged_in?).and_return(Account.where(:username => 'ej0c').first)
ControllerAuthentication.stub!(:login_required).and_return(true)
MyDigisController.stub!( :login_required ).and_return(true)
我认为这意味着我错过了事情的整个理论。我怎样才能让我的登录正常工作?
我尝试按照 Punit 的建议进行如下操作: [pre]
require 'spec_helper'
describe "View event details" do
it "Should show a table of events" do
@account = Account.where(:username => 'ej0c').first
puts @account.inspect
controller.stub!(:current_account).and_return(@account)
controller.stub!(:logged_in?).and_return(true)
session[:account_id] = @account.id
visit '/my_digis/66/custom_events'
page.should have_content('Events')
end
end
@account.inspect 显示得很好,但我也得到了
An expectation of :current_account was set on nil. Called from C:/Users/Ed/webapps/whendidji3/spec/con
.rb:8:in `block (2 levels) in <top (required)>'. Use allow_message_expectations_on_nil to disable warn
An expectation of :logged_in? was set on nil. Called from C:/Users/Ed/webapps/whendidji3/spec/controll
:in `block (2 levels) in <top (required)>'. Use allow_message_expectations_on_nil to disable warnings.
感谢任何详细的解释,因为我已经从高到低进行了搜索以了解发生了什么。
I'm using Ryan Bates's nifty:authentication
, and starting testing with Rspec. Fought with this for weeks, and still don't understand what's happening.
My controller simply calls
before_filter :login_required, :except => [:login]
Which is defined in lib/controller_authentication
def self.included(controller)
controller.send :helper_method, :current_account, :logged_in?, :redirect_to_target_or_default
end
def current_account
@current_account ||= Account.find(session[:account_id]) if session[:account_id]
end
def logged_in?
current_account
end
def login_required
unless logged_in?
store_target_location
redirect_to login_url, :alert => "You must first answer me these riddles three log in or sign up before accessing this page."
end
end
def redirect_to_target_or_default(default, *args)
redirect_to(session[:return_to] || default, *args)
session[:return_to] = nil
end
private
def store_target_location
session[:return_to] = request.url
end
end
The app works as intended, but the testing fails every time. No matter what I try, I get the redirect_to login_url, :alert => "You must first ...log in"
page.
Things I've tried:
controller.stub!( :login_required )
ControllerAuthentication.stub!(:current_account).and_return(Account.where(:username => 'ej0c').first)
#ControllerAuthentication.stub!(:logged_in?).and_return(Account.where(:username => 'ej0c').first)
ControllerAuthentication.stub!(:login_required).and_return(true)
MyDigisController.stub!( :login_required ).and_return(true)
Which I think means I'm missing the whole theory of the thing. How can I make my login work?
I tried as Punit suggests below:
[pre]
require 'spec_helper'
describe "View event details" do
it "Should show a table of events" do
@account = Account.where(:username => 'ej0c').first
puts @account.inspect
controller.stub!(:current_account).and_return(@account)
controller.stub!(:logged_in?).and_return(true)
session[:account_id] = @account.id
visit '/my_digis/66/custom_events'
page.should have_content('Events')
end
end
@account.inspect displayed nicely, but I also got
An expectation of :current_account was set on nil. Called from C:/Users/Ed/webapps/whendidji3/spec/con
.rb:8:in `block (2 levels) in <top (required)>'. Use allow_message_expectations_on_nil to disable warn
An expectation of :logged_in? was set on nil. Called from C:/Users/Ed/webapps/whendidji3/spec/controll
:in `block (2 levels) in <top (required)>'. Use allow_message_expectations_on_nil to disable warnings.
Thanks for any detailed explanations, as I've searched high an low to understand what's goin on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用的是普通规范而不是控制器规范,这意味着未设置变量“控制器”。
要使用控制器规范,您需要将控制器类名称传递到描述块而不是字符串。
请参阅 http://rspec.info/rails/writing/controllers.html
一旦你这样一来,您应该能够使用您最初的想法来存根login_required
You are using a vanilla spec rather than a controller spec, which means that the variable 'controller' is not being set.
To use a controller spec, you need to pass the controller class name to your describe block rather than a string.
see http://rspec.info/rails/writing/controllers.html
Once you have that going, you should be able to use your original thought of stubbing login_required
您需要存根“current_account”方法。你可以这样做 -
你可能应该从上面几行创建一个方法来在需要的地方存根身份验证。
You need to stub the "current_account" method. You could do it like this -
You should probably create a method out of the above lines to stub authentication wherever required.
好的,答案是我需要一个请求规范。
我从请求规范开始,但它不在
requests
文件夹中,而且我提出的问题让它变成了半请求/半控制器规范,但都不起作用。rspec 的奇怪之处在于,如果水豚方法的格式错误,它会抱怨,...但它不会提到这个东西在你放置它的地方不起作用!
我的工作规范位于
specs/requests/my_digis_spec.rb
中,就像宣传的
那样简单
,我只花了 5 周时间就获得了登录部分。谢谢。
OK, the answer was that I needed a request spec.
I'd begun with a request spec, but it wasn't in the
requests
folder, and the questions I asked got it morphed into a half-request/half-controller spec, none of which works.The strange thing about rspec is that it will complain if a capybara method is badly formed,...but it won't mention that the thing just plain doesn't work where you put it!
My working spec, located in
specs/requests/my_digis_spec.rb
isend
end
As easy as advertised, just took me 5 weeks to get the login part. Thanks.