使用 RSpec 存根 ApplicationController 方法
我正在尝试对 rspec 中的控制器进行存根身份验证。当我存根授权方法时,无论我提供什么值,测试总是通过。
控制器:
class FoosController < ApplicationController
before_filter :authorize
...
end
ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
protected
def authorize
return true if current_user
flash[:error] = 'Please login'
redirect_to signin_path
false
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
规格:
# this passes (expected)
it "..." do
controller.stubs(:current_user).returns(User.new)
get :index
response.should be_success
end
# this fails (expected)
it "..." do
controller.stubs(:current_user).returns(nil)
get :index
response.should be_success
end
# this passes (expected)
it "..." do
controller.stubs(:authorize).returns(true)
get :index
response.should be_success
end
# Problem: this passes (unexpected)
it "..." do
controller.stubs(:authorize).returns(false)
get :index
response.should be_success
end
似乎只要我存根:授权,无论设置什么值,它总是通过 before_filter 。我认为这可能是 protected/helper_method 指定,但使用这些并没有改变任何东西。
为什么使用 false 进行存根 :authorize 会导致 before_filter 通过?
I am trying to stub authentication for a controller in rspec. When I stub the authorize
method the test always passed no matter what the value I supply.
Controller:
class FoosController < ApplicationController
before_filter :authorize
...
end
ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
protected
def authorize
return true if current_user
flash[:error] = 'Please login'
redirect_to signin_path
false
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
Specs:
# this passes (expected)
it "..." do
controller.stubs(:current_user).returns(User.new)
get :index
response.should be_success
end
# this fails (expected)
it "..." do
controller.stubs(:current_user).returns(nil)
get :index
response.should be_success
end
# this passes (expected)
it "..." do
controller.stubs(:authorize).returns(true)
get :index
response.should be_success
end
# Problem: this passes (unexpected)
it "..." do
controller.stubs(:authorize).returns(false)
get :index
response.should be_success
end
It seems like as soon as I stub :authorize, no matter what value is set, it always passes the before_filter. I thought it might be the protected/helper_method designations, but playing with those didn't change anything.
Why does stubbing :authorize with false cause the before_filter to pass?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要检查正在渲染的内容。
查看您的代码,如果当
authorize
返回 false 时调用链确实停止,那么将会发生什么呢?
没有重定向或渲染调用。
那么这将是一个空响应?
空响应仍然是 200。
但是,根据您使用的 Rails 版本,在 Rails 3.1 中,返回
false
的before_filter
可能不再停止链。实际上,想要停止链的
before_filter
应该在I think you need to check WHAT is being rendered.
Looking at your code, if the call-chain does indeed stop when
authorize
returns false,then what is going to happen?
There is no redirect, or render call.
So it will be an empty response?
An empty response would still be a 200.
However, depending what version of Rails you're using, its possible in Rails 3.1 that a
before_filter
returningfalse
no longer stops the chain.Really, a
before_filter
that wants to stop the chain should do one of the following会导致 before_filter 通过?
我会回答你的最后一个问题
为什么用 false 存根 : authorize 您明确地带着存根返回。
它工作正常,因为当您将
current_user
存根为false
时,就会完全调用授权。我认为你最后的测试实际上并没有为你测试任何东西,但这只是我的意见。I'll answer your last question
Why does stubbing :authorize with false cause the before_filter to pass?
You're stubbing the method authorize, which literally stops all the code inside of it to be called but returns what you are explicitly returning with the stub.
It is working properly, because when you stub
current_user
tofalse
then the authorize is called completely. I think your lasts tests are not actually testing anything for you but thats just my opinion.