Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Having trouble in testing CanCan
<% if can? :update, article %>
<%= link_to 'Edit', edit_article_path(article) %>
<% end %>
<% if can? :destroy, article %>
<%= link_to 'Destroy', article_path(article), :confirm => 'Are you sure?', :method => :delete %>
<% end %>
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
Fabricator(:article) do
title 'This is a title'
content 'This is the content'
author! { Fabricate(:author) }
categories(:count => 1)
end
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'
@article = Fabricate(:article, :author_id => @author.id)
visit articles_path
end
it { should have_link 'Edit', :href => edit_article_path(@article) }
it { should have_link 'Destroy', :method => :delete, :href => article_path(@article) }
# I keep getting this error
#
# Failure/Error: it { should have_link 'Edit', :href => edit_article_path(@article) }
# expected link "Edit" to return something
#
# Failure/Error: it { should have_link 'Destroy', :method => :delete, :href => article_path(@article) }
# expected link "Destroy" to return something
#
# It works in the browser though :(
<%= render @articles %>
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
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
@bryanbibat

This comment has been minimized.

Copy link

bryanbibat commented Apr 10, 2012

Can you commit this code? It seems to work fine on my end when I added it manually.

bry@ubuntu:~/devcon$ bundle exec guard
Guard uses NotifySend to send notifications.
Guard is now watching at '/home/bry/devcon'
Bundle already up-to-date
Starting Spork for RSpec
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
Spork server for RSpec successfully started
Guard::RSpec is running, with RSpec 2!
Running all specs
Running tests with args ["--drb", "--format", "progress", "--color", "-r", "/home/bry/.rvm/gems/ruby-1.9.3-p125@devcon/gems/guard-rspec-0.7.0/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
/home/bry/devcon/app/models/user.rb:33: warning: already initialized constant ROLES
................................................................*.................................................................

Pending:
  Categories pages for signed-in users in the show page should have a list of articles under it
    # Not yet implemented
    # ./spec/requests/categories_pages_spec.rb:50

Finished in 19.15 seconds
130 examples, 0 failures, 1 pending
Done.

> 

@terenceponce

This comment has been minimized.

Copy link
Owner Author

terenceponce commented Apr 11, 2012

I have commited the latest changes to a separate branch. This is the commit that includes those changes. I have completely rewritten the tests, and for some reason, the tests are failing because of CanCan even though it works fine on my browser.

@bryanbibat

This comment has been minimized.

Copy link

bryanbibat commented Apr 11, 2012

See https://github.com/terenceponce/devcon/pull/14
Basically the fabricator for :article doesn't have an author_id entry so the :author_id => @author.id is ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.