2013-10-31 48 views
2

我在学习RSpec,并且在理解共享主题方面有点麻烦,以及它如何与新期望语法一起工作。使用新的Rspec语法在CanCan中测试功能

我跟着tutorial from the cancan wiki,但我不知道如何测试用户不应该能够执行相同语法的操作。

user_spec.rb:

require 'spec_helper' 
require "cancan/matchers" 

describe User do 

    describe 'abilities' do 
    let(:user) { FactoryGirl.create(:user) } 
    let(:ability) { FactoryGirl.create(:user) } 

    subject(:ability) { Ability.new(user) } 

    it { should be_able_to :read, User.new } 

    # clunky expectation 
    it 'should not be able to destroy others' do 
     expect(ability).not_to be_able_to(:destroy, User.new) 
    end 
    end 
end 

我想要做的是写像

it { should not be_able_to :delete, User.new } 

我要对这个错误的期待?

回答

5

可以使用should_not语法在缩短的版本:

it { should_not be_able_to :delete, User.new } 

或替代,使用RSpec 3的同义词:

it { is_expected.not_to be_able_to :delete, User.new }