add be_able_to matcher

This commit is contained in:
David Chelimsky 2010-04-13 21:02:39 +08:00 committed by Ryan Bates
parent 35c4864de4
commit cf49c5b9de
3 changed files with 39 additions and 0 deletions

13
lib/cancan/matchers.rb Normal file
View File

@ -0,0 +1,13 @@
Spec::Matchers.define :be_able_to do |action, subject|
match do |model|
model.can?(action, subject)
end
failure_message_for_should do |model|
"expected to be able to #{action.inspect} #{subject.inspect}"
end
failure_message_for_should_not do |model|
"expected not to be able to #{action.inspect} #{subject.inspect}"
end
end

View File

@ -0,0 +1,25 @@
require "spec_helper"
describe "be_able_to" do
it "delegates to can?" do
object = Object.new
mock(object).can?(:read, 123) { true }
object.should be_able_to(:read, 123)
end
it "reports a nice failure message for should" do
object = Object.new
mock(object).can?(:read, 123) { false }
expect do
object.should be_able_to(:read, 123)
end.should raise_error('expected to be able to :read 123')
end
it "reports a nice failure message for should not" do
object = Object.new
mock(object).can?(:read, 123) { true }
expect do
object.should_not be_able_to(:read, 123)
end.should raise_error('expected not to be able to :read 123')
end
end

View File

@ -5,6 +5,7 @@ require 'active_record'
require 'action_controller'
require 'action_view'
require 'cancan'
require 'cancan/matchers'
Spec::Runner.configure do |config|
config.mock_with :rr