Skip to content
This repository
Browse code

add be_able_to matcher

  • Loading branch information...
commit cf49c5b9deb357b810644d02c577d1738d911464 1 parent 35c4864
David Chelimsky authored April 13, 2010 ryanb committed April 16, 2010
13  lib/cancan/matchers.rb
... ...
@@ -0,0 +1,13 @@
  1
+Spec::Matchers.define :be_able_to do |action, subject|
  2
+  match do |model|
  3
+    model.can?(action, subject)
  4
+  end
  5
+
  6
+  failure_message_for_should do |model|
  7
+    "expected to be able to #{action.inspect} #{subject.inspect}"
  8
+  end
  9
+
  10
+  failure_message_for_should_not do |model|
  11
+    "expected not to be able to #{action.inspect} #{subject.inspect}"
  12
+  end
  13
+end
25  spec/cancan/matchers_spec.rb
... ...
@@ -0,0 +1,25 @@
  1
+require "spec_helper"
  2
+
  3
+describe "be_able_to" do
  4
+  it "delegates to can?" do
  5
+    object = Object.new
  6
+    mock(object).can?(:read, 123) { true }
  7
+    object.should be_able_to(:read, 123)
  8
+  end
  9
+
  10
+  it "reports a nice failure message for should" do
  11
+    object = Object.new
  12
+    mock(object).can?(:read, 123) { false }
  13
+    expect do
  14
+      object.should be_able_to(:read, 123)
  15
+    end.should raise_error('expected to be able to :read 123')
  16
+  end
  17
+
  18
+  it "reports a nice failure message for should not" do
  19
+    object = Object.new
  20
+    mock(object).can?(:read, 123) { true }
  21
+    expect do
  22
+      object.should_not be_able_to(:read, 123)
  23
+    end.should raise_error('expected not to be able to :read 123')
  24
+  end
  25
+end
1  spec/spec_helper.rb
@@ -5,6 +5,7 @@
5 5
 require 'action_controller'
6 6
 require 'action_view'
7 7
 require 'cancan'
  8
+require 'cancan/matchers'
8 9
 
9 10
 Spec::Runner.configure do |config|
10 11
   config.mock_with :rr

0 notes on commit cf49c5b

Please sign in to comment.
Something went wrong with that request. Please try again.