Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

NoMethodError: undefined method `be_able_to' when testing abilities with rspec on rails 3 #220

Closed
apraditya opened this Issue · 3 comments

4 participants

@apraditya

Trying to write test for Ability using RSpec. Here's what I understand from the wiki about testing the ability. So here's my rspec file

require 'spec_helper'
require 'cancan/matchers'

describe Ability do

  describe "as an admin user" do
    before(:each) do
      @user = @user ||= Factory.create(:admin)
      @ability = Ability.new(@user)
    end

    @ability.should be_able_to(:manage, :all)
  end

  describe "As a provider user" do
    before(:each) do
      @user = @user ||= Factory.create(:provider)
      @ability = Ability.new(@user)
    end

    @ability.should be_able_to(:manage, Publisher.new(:user => @user))
  end

end

It seems that cancan matchers is not load properly as I got NoMethodError when running the spec:

Exception encountered: #<NoMethodError: undefined method `be_able_to' for #<Class:0x105949890>>
backtrace:
/Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/remarkable-4.0.0.alpha4/lib/remarkable/core/macros.rb:15:in `method_missing'
/Users/apraditya/Work/Personal_Project/Repository_Sunnah/main-repo/spec/models/ability_spec.rb:12
/Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:131:in `module_eval'
/Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:131:in `subclass'
/Users/apraditya/.rvm/gems/ruby-1.8.7-p299/gems/rspec-core-2.3.1/lib/rspec/core/example_group.rb:118:in `describe'

I'm using cancan 1.4.1 on Rails 3.0.3. Did I miss something? How should the test be done? Thank you for the great gem! It is really neat and awesome one.

@ryanb
Owner

You'll need to place the matcher inside an it call.

describe Ability do

  describe "as an admin user" do
    before(:each) do
      @user = @user ||= Factory.create(:admin)
      @ability = Ability.new(@user)
    end

    it "should be able to manage all" do
      @ability.should be_able_to(:manage, :all)
    end
  end

  describe "As a provider user" do
    before(:each) do
      @user = @user ||= Factory.create(:provider)
      @ability = Ability.new(@user)
    end

    it "should be able to manage new publishers he owns" do
      @ability.should be_able_to(:manage, Publisher.new(:user => @user))
    end
  end

end
@maxydon

Sorry but still not working for me.. same error (i even copy pasted your code..)

NoMethodError:
undefined method `able_to?' for #Ability:0x007fa484739c10

@pietbrauer

I ran into this problem, too. You need require "cancan/matchers" .

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.