Last Updated: February 25, 2016
·
3.947K
· caulfield

Testing rails cancan abilities with rspec

Simple template for testing cancan abilities in rails project using rspec goodness

shared_examples "guest" do
  it { expect(user).to have_ability([:show, :index], for: User) }
end

shared_examples "user" do
  it { expect(user).to have_ability(:edit, for: user) } 
end

describe "Abilities" do
  describe "Guest" do
    let(:user) { nil}

    it_behaves_like "guest"
  end

  describe "User" do
    let(:user) { Fabricate(:user) }

    it_behaves_like "guest"
    it_behaves_like "user"
  end
end

CanCan testing wiki

1 Response
Add your response