使用新的Rspec语法测试CanCan中的能力

max*_*max 2 rspec ruby-on-rails devise cancan

我正在学习RSpec,我在理解共享主题方面遇到了一些麻烦,以及它如何与新的期望语法一起工作.

我遵循了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
Run Code Online (Sandbox Code Playgroud)

我想做的就是写下一个期望

it { should not be_able_to :delete, User.new }
Run Code Online (Sandbox Code Playgroud)

我错了吗?

Sco*_*man 6

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

it { should_not be_able_to :delete, User.new }
Run Code Online (Sandbox Code Playgroud)

或者,使用RSpec 3的同义词:

it { is_expected.not_to be_able_to :delete, User.new }
Run Code Online (Sandbox Code Playgroud)