Rails 初学者使用 CanCan 测试 RSpec 的问题

发布于 2024-12-01 19:56:36 字数 1828 浏览 0 评论 0原文

我目前正在使用 cancan 和 rspec。

请看看我的ability.rb

require 'spec_helper'
require "cancan/matchers"

class Ability

  include CanCan::Ability

  def initialize(user)
    if user       # Only logged in users
      if user.role? :admin
        can :manage, :all

      elsif user.role? :producer
        can :read, Business
        can :update, Business do |b|
          b.user_id == user.id
        end
        can :redeem, Purchase 

      elsif user.role? :consumer
        can :your, Deal
        can [:create, :redirect_to_wepay], Purchase
        can :show, Purchase do |purchase|
          purchase.user_id == user.id
        end
      end

      # Good thing about devise with Cancan is that it takes care of this.
      can :manage, User do |the_user|
        the_user.id == user.id
      end

    else
      # This is needed for the cans that follows
      user = User.new

    end

    # Everyone's session
    can :read, Deal
    can :read, Business

    # You have to enable it for wepay
    can [:sold_out, :callback, :received], Purchase

  end
end

在我的spec/models/ability_spec.rb 中,我有

describe Ability do 

  describe "consumers" do
    describe "cancan" do 
      before(:each) do
        @user = Factory(:user, :role => "consumer")
        @ability = Ability.new(@user)
      end

      describe "success" do 
        #**This line I am getting ability is nil
        @ability.should == 5

        #**This line gives me be_able_to undefined
        #@ability.should_not be_able_to(:read, Factory(:deal))

        #@ability.can(:read, Factory(:business)).should be_true
      end

任何想法为什么我将@ability 设为nil?

另外,我想将我的控制器的一些与权限控制相关的操作放在这个ability_spec.rb文件中。这可能吗? (我明确希望实现这一目标,因为我的应用程序有 3 个用户角色,而且我发现自己在控制器规范文件中乱扔了所有这些与权限相关的衬里。

谢谢!

I am currently using cancan with rspec.

Please take a look at my ability.rb

require 'spec_helper'
require "cancan/matchers"

class Ability

  include CanCan::Ability

  def initialize(user)
    if user       # Only logged in users
      if user.role? :admin
        can :manage, :all

      elsif user.role? :producer
        can :read, Business
        can :update, Business do |b|
          b.user_id == user.id
        end
        can :redeem, Purchase 

      elsif user.role? :consumer
        can :your, Deal
        can [:create, :redirect_to_wepay], Purchase
        can :show, Purchase do |purchase|
          purchase.user_id == user.id
        end
      end

      # Good thing about devise with Cancan is that it takes care of this.
      can :manage, User do |the_user|
        the_user.id == user.id
      end

    else
      # This is needed for the cans that follows
      user = User.new

    end

    # Everyone's session
    can :read, Deal
    can :read, Business

    # You have to enable it for wepay
    can [:sold_out, :callback, :received], Purchase

  end
end

In my spec/models/ability_spec.rb I have

describe Ability do 

  describe "consumers" do
    describe "cancan" do 
      before(:each) do
        @user = Factory(:user, :role => "consumer")
        @ability = Ability.new(@user)
      end

      describe "success" do 
        #**This line I am getting ability is nil
        @ability.should == 5

        #**This line gives me be_able_to undefined
        #@ability.should_not be_able_to(:read, Factory(:deal))

        #@ability.can(:read, Factory(:business)).should be_true
      end

Any ideas why I am getting @ability as nil?

In addition, I want to put some of my controller's actions that are related to permission control in this ability_spec.rb file. Is that possible? (I explicitly want to achieve this because my app has 3 roles of users and I find myself littering my controllers spec files with all these permission related one liners.

Thanks!

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

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

发布评论

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

评论(1

七色彩虹 2024-12-08 19:56:36

测试必须出现在 itspecify 块中。 describecontext 只是用于分组。

describe "success" do 
  #**This line I am getting ability is nil
  @ability.should == 5
end

应该更像是:

it "allows consumers to do blah blah blah" do 
  @ability.should == 5
end

Tests must appear in it or specify blocks. describe and context are simply for grouping.

describe "success" do 
  #**This line I am getting ability is nil
  @ability.should == 5
end

Should be more like:

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