RSpec 和 CanCan 控制器测试

发布于 2024-10-08 19:05:20 字数 1266 浏览 1 评论 0原文

我在项目中使用 RSpec 和 CanCan。我正在与能力类相关的规范中测试我的权限逻辑。对于控制器,我真的只想确保我正在进行授权检查。我设置了一个控制器宏,但它似乎无法正常工作。

所以我真的有两个问题。一,该策略是否足以测试我的控制器的权限逻辑(或者我应该更多地测试控制器授权逻辑)?

第二,有人看到我做错了什么而使它不起作用吗?

#plan_orders_controller.rb
def approve
  plan_order = PlanOrder.find(params[:id])
  authorize! :update, plan_order
  current_user.approve_plan_order(plan_order)
  redirect_to plan_order_workout_plan_url(plan_order)
end

#controller_macros.rb
def it_should_check_permissions(*actions)
  actions.each do |action|
    it "#{action} action should authorize user to do this action" do
      @plan_order = Factory(:plan_order, :id=>1)
      ability = Object.new
      ability.extend(CanCan::Ability)
      controller.stub!(:current_ability).and_return(ability)        

      get action, :id => 1
      ability.should_receive(:can?)
    end
  end    
end

我从 RSpec 得到的输出如下

 Failure/Error: ability.should_receive(:can?)
 (#<Object:0x00000006d4fa20>).can?(any args)
     expected: 1 time
     received: 0 times
 # ./spec/controllers/controller_macros/controller_macros.rb:27:in `block (2 levels) in it_should_check_permissions'

我不太确定当我在控制器中调用 !authorize 时应该检查哪种方法(或者它是通过 load_and_authorize_resource 自动调用的)

I'm using RSpec and CanCan in a project. I'm testing my permission logic in specs related to the Ability class. For the controllers I really just want to make sure I'm doing an authorization check. I set up a controller macro, but it doesn't seem to be working correctly.

So really I have two questions. One, is the strategy sufficient for testing the permission logic of my controllers (or should I be testing the controller authorization logic more)?

Two, does anyone see what I'm doing wrong to make this not work?

#plan_orders_controller.rb
def approve
  plan_order = PlanOrder.find(params[:id])
  authorize! :update, plan_order
  current_user.approve_plan_order(plan_order)
  redirect_to plan_order_workout_plan_url(plan_order)
end

#controller_macros.rb
def it_should_check_permissions(*actions)
  actions.each do |action|
    it "#{action} action should authorize user to do this action" do
      @plan_order = Factory(:plan_order, :id=>1)
      ability = Object.new
      ability.extend(CanCan::Ability)
      controller.stub!(:current_ability).and_return(ability)        

      get action, :id => 1
      ability.should_receive(:can?)
    end
  end    
end

The output I get from RSpec is the following

 Failure/Error: ability.should_receive(:can?)
 (#<Object:0x00000006d4fa20>).can?(any args)
     expected: 1 time
     received: 0 times
 # ./spec/controllers/controller_macros/controller_macros.rb:27:in `block (2 levels) in it_should_check_permissions'

I'm not really sure which method I should be checking when I call !authorize in the controller (or it is automatically called via load_and_authorize_resource)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

如果没结果 2024-10-15 19:05:20

should_receive 是对未来发生的事情的期望。反转这两行:

get action, :id => 1
ability.should_receive(:can?)

所以你有这个:

ability.should_receive(:can?)
get action, :id => 1

should_receive is an expectation of something that happens in the future. Reverse these two lines:

get action, :id => 1
ability.should_receive(:can?)

so you have this:

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