Created
February 9, 2012 20:25
-
-
Save justincampbell/1782830 to your computer and use it in GitHub Desktop.
CanCan Ability Spec
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
require 'spec_helper' | |
describe Ability do | |
let(:admin_user) { mock AdminUser } | |
it { Ability.should include(CanCan::Ability) } | |
it { Ability.should respond_to(:new).with(1).argument } | |
context "admin" do | |
before :each do | |
admin_user.stub!(:role).and_return("admin") | |
end | |
it "can manage all" do | |
Ability.any_instance.should_receive(:can).with(:manage, :all) | |
Ability.new admin_user | |
end | |
end | |
context "editor" do | |
before :each do | |
admin_user.stub!(:role).and_return("editor") | |
end | |
it "can not manage all" do | |
Ability.any_instance.should_not_receive(:can).with(:manage, :all) | |
Ability.new admin_user | |
end | |
it "can create and edit models" do | |
[Thing, Widget].each do |model| | |
Ability.any_instance.should_receive(:can).with([:read, :update], model) | |
end | |
Ability.new admin_user | |
end | |
end | |
end |
@atomical It sounds like you don't have rspec loaded correctly
At the top:
require 'spec_helper'
require "cancan/matchers"
Would it be a matcher?
.should_receive
is a method from rspec-expectations.
Honestly the code above is probably not the best way to do this. Take a look here: https://github.com/ryanb/cancan/wiki/Testing-Abilities
I'm writing a test to make sure a special form of current_user is passed
in. Basically testing to make sure the right arguments are passed in in
current_ability in the application controller.
On Tue, Mar 13, 2012 at 10:31 AM, Justin Campbell < ***@***.*** > wrote:
`.should_receive` is a method from rspec-expectations.
Honestly the code above is probably not the best way to do this. Take a
look here: https://github.com/ryanb/cancan/wiki/Testing-Abilities
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1782830
##
url: http://www.adamhallett.com
Updated the gist with our latest code. I think .any_instance
will solve the problem you were having. Hope this helps.
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get this output when I try to use should_receive:
undefined method `should_receive' for Ability:Class (NoMethodError)