使用 RSpec 存根 ApplicationController 方法

发布于 2024-12-04 05:49:14 字数 1326 浏览 0 评论 0原文

我正在尝试对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榕城若虚 2024-12-11 05:49:14

我认为您需要检查正在渲染的内容。

查看您的代码,如果当 authorize 返回 false 时调用链确实停止,
那么将会发生什么呢?

没有重定向或渲染调用。

那么这将是一个空响应?

空响应仍然是 200。

但是,根据您使用的 Rails 版本,在 Rails 3.1 中,返回 falsebefore_filter 可能不再停止链。

实际上,想要停止链的 before_filter 应该在

  1. 某处执行以下重定向
  2. 之一:渲染某些内容
  3. 引发某些内容

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 returning false no longer stops the chain.

Really, a before_filter that wants to stop the chain should do one of the following

  1. redirect somewhere
  2. render something
  3. raise something
jJeQQOZ5 2024-12-11 05:49:14

会导致 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 to false then the authorize is called completely. I think your lasts tests are not actually testing anything for you but thats just my opinion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文