Rails 3:使用cancan的自动资源加载时如何创建rspec控制器规格?

发布于 2024-12-14 19:49:44 字数 499 浏览 4 评论 0原文

我在这里遗漏了一些东西......

Cancan 有一个巧妙的功能来自动加载您的资源:(

取自 https://github.com/ryanb/cancan/wiki/Authorizing-Controller-Actions

def index
  # @products automatically set to Product.accessible_by(current_ability)
end

除了我不知道如何让它在我的 rspec 控制器规范中工作......

这可能非常简单我现在只是脑子很乱。 :P 无论如何,它来自cancan的load_and_authorize_resource函数。

有想法吗?谢谢!

I'm missing something here....

Cancan has a neat feature to automatically load your resources:

(taken from https://github.com/ryanb/cancan/wiki/Authorizing-Controller-Actions)

def index
  # @products automatically set to Product.accessible_by(current_ability)
end

Except I can't figure out how to get this to work in my rspec controller specs...

It's probably very simple and I'm just brain-frazzled at the moment. : P Anyway, it comes from cancan's load_and_authorize_resource function.

Ideas? Thanks!

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

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

发布评论

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

评论(1

梦途 2024-12-21 19:49:44

测试此控制器的最佳方法是使用 current_user 方法进行存根。

例如,我们有产品模型和产品控制器。

class Product < ActiveRecord::Base 
end

class ProductsContoller < ApplicationController
  load_and_authorize_resource

  def index
  end

end

# allow access to Product only for admin user
class Ability
  include CanCan::Ability

  def initialize(user)

    user ||= User.new

    if user.admin?
      can :manage, Product
    end  

  end
end


# stubbing current_user method
module ControllersHelper
  def current_user!(user)
    raise "argument not valid" unless user.is_a?(User)
    controller.stub!(:current_user).and_return(user)
  end
end

# and include this method to rspec config
Spec::Runner.configure do |config|
  config.include ControllersHelper, :type => :controller
end


describe ProductsContoller do
  before do
    current_user!(user)
  end
  let(:user) { Factory(:user) }

  describe "GET index" do

    describe "for admin users" do
      before do
        user.update_attribute(:admin, true)
      end  

      it "should find .products tag" do
        get products_path
        response.should have_tag ".products"
      end  
    end  

    describe "for guest users" do
      it "should find .products tag" do
        get products_path
        response.should_not be_success
      end  
    end
  end
end

The best way to test this controller is a stub the current_user method.

For instance, we have the Product model and Products controller.

class Product < ActiveRecord::Base 
end

class ProductsContoller < ApplicationController
  load_and_authorize_resource

  def index
  end

end

# allow access to Product only for admin user
class Ability
  include CanCan::Ability

  def initialize(user)

    user ||= User.new

    if user.admin?
      can :manage, Product
    end  

  end
end


# stubbing current_user method
module ControllersHelper
  def current_user!(user)
    raise "argument not valid" unless user.is_a?(User)
    controller.stub!(:current_user).and_return(user)
  end
end

# and include this method to rspec config
Spec::Runner.configure do |config|
  config.include ControllersHelper, :type => :controller
end


describe ProductsContoller do
  before do
    current_user!(user)
  end
  let(:user) { Factory(:user) }

  describe "GET index" do

    describe "for admin users" do
      before do
        user.update_attribute(:admin, true)
      end  

      it "should find .products tag" do
        get products_path
        response.should have_tag ".products"
      end  
    end  

    describe "for guest users" do
      it "should find .products tag" do
        get products_path
        response.should_not be_success
      end  
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文