Browse files

Added should observe. Patch by Luke Melia.

  • Loading branch information...
1 parent ce4a82a commit 4e88e05cfcf4ef79103bf270a4044bc153efbae7 @joshknowles committed Mar 3, 2008
Showing with 46 additions and 2 deletions.
  1. +1 −0 CHANGELOG
  2. +8 −1 README
  3. +3 −1 init.rb
  4. +34 −0 lib/spec/rails/matchers/observers.rb
View
1 CHANGELOG
@@ -4,5 +4,6 @@ Change Log
Trunk
-----
+* 2008/03/02 - Added should observe (Luke Melia)
* 2008/03/02 - Patched validates_length_of to use within to be consistent with Rails (Matt Pelletier)
* 2007/01/03 - Initial Public Release
View
9 README
@@ -51,6 +51,11 @@ Adds the following RSpec matchers:
object.should validate_length_of(:attribute, :is => 5)
TM snippet: [msvl + tab]
+ * Observers:
+ Verify that the observer is observing a class. (doesn't verify that the observation works)
+
+ object.should observe(:model)
+ example: GroupObserver.should observe(Group)
* Views:
Verifies that the views contains some tags.
@@ -173,6 +178,8 @@ Core Contributors
Contributors
-------------
- * ckknight (improved should validate_length_of)
+ * ckknight
+ * Matt Pelletier
+ * Luke Melia
Copyright (c) 2008 The Plugin Development Team, released under the MIT license
View
4 init.rb
@@ -1,3 +1,5 @@
+require 'spec/rails/matchers/observers'
require 'spec/rails/matchers/associations'
require 'spec/rails/matchers/validations'
-require 'spec/rails/matchers/views'
+require 'spec/rails/matchers/views'
+require 'spec/rails/matchers/observers'
View
34 lib/spec/rails/matchers/observers.rb
@@ -0,0 +1,34 @@
+module Spec
+ module Rails
+ module Matchers
+
+ class Observe
+ def initialize(expected_model_class)
+ @expected_model_class = expected_model_class
+ end
+
+ def matches?(observer)
+ @observer = observer
+ if @observer.is_a?(ActiveRecord::Observer)
+ @observer = @observer.class
+ end
+ @observed_classes = observer.observed_classes.flatten
+ @observed_classes.include?(@expected_model_class)
+ end
+
+ def failure_message
+ return "expected #{@observer.name} to observe #{@expected_model_class.name}, but it was not included in [#{@observed_classes.map(&:name).join(', ')}]"
+ end
+
+ def description
+ "observer to be observing #{@expected_model_class.name}"
+ end
+ end
+
+ def observe(expected_model_class)
+ Observe.new(expected_model_class)
+ end
+
+ end
+ end
+end

0 comments on commit 4e88e05

Please sign in to comment.