Search code examples
ruby-on-railsrspeccancan

Rspec : Custom Matchers : want a matcher with behavior just opposite of existing matcher


I have a situation

let(:user) { create(:user, organization: org, role_ids: [Role::ROLE_CSA]) }
subject(:ability) { Ability.new(user) }
describe 'Not Permitted' do

  # I need this
  it { is_expected_not_to (be_able_to(:index, AdminsController)) }

  # Or this
  it { is_expected_to (not_be_able_to(:index, AdminsController)) }
end

But unfortunately I found that the is_expected_not_to and not_be_able_to are not available.

I could do in this way

  it 'should not permit' do
    expect(ability).not_to be_able_to(:index, AdminsController)
  end

But If possible I would like to use the short form instead. Is there any way I can achieve what I want?

If not, I would like to create new custom matcher which has behavior just opposite to be_able_to of CanCan or is_expected_to if possible. can anybody help?


Solution

  • from: https://www.relishapp.com/rspec/rspec-core/docs/subject/one-liner-syntax

    You should be able to do:

    it { is_expected.not_to (be_able_to(:index, AdminsController)) }