Search code examples
ruby-on-railsrspecdevisecancan

Testing abilities in CanCan with new Rspec syntax


I'm learning RSpec and I'm having a little trouble understanding shared subjects, and how it works with the new expect syntax.

I followed a tutorial from the cancan wiki but i can't figure out how to test when a user should not be able to perform an action with the same syntax.

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

What i want to do is write an expectation like

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

Am I going about this wrong?


Solution

  • You can use the should_not syntax in your shortened version:

    it { should_not be_able_to :delete, User.new }
    

    or alternatively, with RSpec 3's synonyms:

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