Question

I am testing for the presence of a link that's controlled by CanCan:

before do
  @author = Fabricate(:author)
  visit new_user_session_path
  fill_in 'Email', :with => @author.email
  fill_in 'Password', :with => @author.password
  click_button 'Sign in'
  visit articles_path
end

it { should have_link 'New article', :href => new_article_path }

This is the view that it's testing:

<% if can? :create, @articles %>
  <%= link_to 'New article', new_article_path %>
<% end %>

When I run the test, it fails and produces this error:

Failure/Error: it { should have_link 'New article', :href => new_article_path }

   expected link "New article" to return something

This is weird because it works when I manually test it in my browser. Here's the ability class:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # Guest user

    if user.role? :admin
      can :manage, :all
    else
      can :read, :all

      if user.role?(:author)
        can :create, Article
        can :update, Article do |article|
          article.try(:author) == user
        end
        can :destroy, Article do |article|
          article.try(:author) == user
        end
      end
    end
  end
end

For more context, here's my user and user_fabricator class:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me
  attr_accessible :name, :roles

  has_many :articles, :foreign_key => 'author_id', :dependent => :destroy

  ROLES = %w[admin moderator author]

  def roles=(roles)
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
  end

  def roles
    ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
  end

  def role?(role)
    roles.include?(role.to_s)
  end
end

The user roles methods are based on Ryan Bates' methods on one of his RailsCasts episodes. Here's the fabricator:

Fabricator(:user) do
  email { sequence(:email) { |i| "user#{i}@example.com" } }
  name { sequence(:name) { |i| "Example User-#{i}" } }
  password 'foobar'
end

Fabricator(:admin, :from => :user) do
  roles ['admin']
end

Fabricator(:author, :from => :user) do
  roles ['author']
end

I have a hunch that it has something to do with how I defined the ability class, but I can't find where I went wrong. Any help will be much appreciated. Thanks :)

Was it helpful?

Solution

In the view, where you do

if can? :create, @articles

I guess you're passing an array to can?, which seems a bit odd.

As far as I know, the can? method expects either a symbol or a class.

Try replacing @articles with Article or :articles.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top