Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 882
max Avatar asked Oct 20 '25 01:10

max


1 Answers

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 }
like image 125
Scott Matthewman Avatar answered Oct 21 '25 15:10

Scott Matthewman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!