Rails 初学者使用 CanCan 测试 RSpec 的问题
我目前正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
测试必须出现在
it
或specify
块中。describe
和context
只是用于分组。应该更像是:
Tests must appear in
it
orspecify
blocks.describe
andcontext
are simply for grouping.Should be more like: