Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added should observe. Patch by Luke Melia.

git-svn-id: https://rspec-on-rails-matchers.googlecode.com/svn/trunk@23 11e6f158-dd3c-0410-a69b-4d739fb74d9d
  • Loading branch information...
commit 33f44e4d7242d05fa9f1a25556e0f4d11b713feb 1 parent 1b2d79a
joshknowles authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.