Created
April 10, 2012 08:56
-
-
Save terenceponce/2349494 to your computer and use it in GitHub Desktop.
Having trouble in testing CanCan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <% 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Fabricator(:article) do | |
| title 'This is a title' | |
| content 'This is the content' | |
| author! { Fabricate(:author) } | |
| categories(:count => 1) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 :( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%= render @articles %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
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.
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
Can you commit this code? It seems to work fine on my end when I added it manually.