From cf49c5b9deb357b810644d02c577d1738d911464 Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Tue, 13 Apr 2010 21:02:39 +0800 Subject: [PATCH] add be_able_to matcher --- lib/cancan/matchers.rb | 13 +++++++++++++ spec/cancan/matchers_spec.rb | 25 +++++++++++++++++++++++++ spec/spec_helper.rb | 1 + 3 files changed, 39 insertions(+) create mode 100644 lib/cancan/matchers.rb create mode 100644 spec/cancan/matchers_spec.rb diff --git a/lib/cancan/matchers.rb b/lib/cancan/matchers.rb new file mode 100644 index 0000000..ef764e6 --- /dev/null +++ b/lib/cancan/matchers.rb @@ -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 diff --git a/spec/cancan/matchers_spec.rb b/spec/cancan/matchers_spec.rb new file mode 100644 index 0000000..622ae69 --- /dev/null +++ b/spec/cancan/matchers_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d500c42..4e670be 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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