我正在学习rspec,想从“设计”中测试我的能力课程,这段代码:
class Ability
include CanCan::Ability
def initialize(user)
@user = user || User.new
我目前的rspec看起来像这样:
require 'spec_helper.rb'
describe Ability do
let(:user) { User.new }
let(:ability) { Ability.new(user) }
it 'should be an instance of Abiliy' do
# expect(@user).to be_an_instance_of User # also works
ability.should be_an_instance_of Ability
# passes test
end
it "should include CanCan::Ability" do
expect(:ability).to include(CanCan::Ability)
# NoMethodError: undefined method `include?'
end
it 'should initialize with a User' do
pending
end
it 'should have @user instance variable' do
ability.user.should == User
# NoMethodError: undefined method `user'
end
有人可以提供一个例子。我做了一项研究但没有成功......
答案 0 :(得分:1)
第一期=> include
匹配器可以处理响应include?
的任何对象(例如字符串或数组)
module M;end
class C
include M
end
class D;end
C.new.include?(M)
NoMethodError: undefined method `include?' for #<C:0x2c5fa58>
from (irb):10
from C:/Ruby200/bin/irb:12:in `<main>'
C.include?(M) #=>true
D.include?(M) #=>false
C.respond_to?(:include?) #=>true
C.new.respond_to?(:include?) #=>false
答案 1 :(得分:0)
如果你正在学习和玩耍,那么测试CanCan并没有什么坏处。但是,如果您正在处理项目,则不需要这些测试。
CanCan详细介绍了单元测试,特别是spec/cancan/ability_spec.rb
。你所做的是以另一种方式复制Ryan的工作:)你也可以打开宝石,看看它是如何测试的。
对于使用CanCan,我的建议是测试控制器级别的集成效果,比如你不允许某些角色阅读,然后获取它并被拒绝,等等。