Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated to new matcher syntax http://github.com/dchelimsky/rspec/wiki…

  • Loading branch information...
commit 787d8466afa0f296a6a3dc88e6849927073d031c 1 parent 595e57e
nodanaonlyzuul authored
View
6 README
@@ -13,7 +13,7 @@ Dependencies:
Overview
--------
-Adds the following RSpec matchers:
+Adds the following RSpec matchers using the new Matcher syntax (http://github.com/dchelimsky/rspec/wiki/custom-matchers):
* Associations:
Verify that the association has been defined. (doesn't verify that the association works!)
@@ -53,7 +53,7 @@ Adds the following RSpec matchers:
* 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)
@@ -64,7 +64,7 @@ Adds the following RSpec matchers:
TM snippet: [hfpt + tab]
response.should have_form_putting_to(url_or_path)
-
+
response.should have_text_field_for(:attribute)
TM snippet: [htff + tab]
View
12 init.rb
@@ -1,6 +1,6 @@
-require 'spec/matchers/simple_matcher'
-require 'spec/rails/matchers/observers'
-require 'spec/rails/matchers/associations'
-require 'spec/rails/matchers/validations'
-require 'spec/rails/matchers/views'
-require 'spec/rails/matchers/observers'
+if Rails.env == "test"
+ require 'spec/rails/matchers/observers'
+ require 'spec/rails/matchers/associations'
+ require 'spec/rails/matchers/validations'
+ require 'spec/rails/matchers/views'
+end
View
72 lib/spec/rails/matchers/associations.rb
@@ -1,53 +1,27 @@
-module Spec
- module Rails
- module Matchers
- def belong_to(association)
- return simple_matcher("model to belong to #{association}") do |model|
- model = model.class if model.is_a? ActiveRecord::Base
- model.reflect_on_all_associations(:belongs_to).find { |a| a.name == association }
- end
- end
-
- def have_many(association)
- return simple_matcher("model to have many #{association}") do |model|
- model = model.class if model.is_a? ActiveRecord::Base
- model.reflect_on_all_associations(:has_many).find { |a| a.name == association }
- end
- end
-
- def have_one(association)
- return simple_matcher("model to have one #{association}") do |model|
- model = model.class if model.is_a? ActiveRecord::Base
- model.reflect_on_all_associations(:has_one).find { |a| a.name == association }
- end
- end
-
- def have_and_belong_to_many(association)
- return simple_matcher("model to have and belong to many #{association}") do |model|
- model = model.class if model.is_a? ActiveRecord::Base
- model.reflect_on_all_associations(:has_and_belongs_to_many).find { |a| a.name == association }
- end
- end
-
- class HaveValidAssociationMatcher
- def matches?(model)
- @failed_association = nil
- @model_class = model.class
-
- model.class.reflect_on_all_associations.each do |assoc|
- model.send(assoc.name) rescue @failed_association = assoc.name
- end
- !@failed_association
- end
+RSpec::Matchers.define :belong_to do |association|
+ match do |object|
+ object = object.class if object.is_a? ActiveRecord::Base
+ object.reflect_on_all_associations(:belongs_to).find { |a| a.name == association }
+ end
+end
- def failure_message
- "invalid or nonexistent association \"#{@failed_association}\" on #{@model_class}"
- end
- end
+RSpec::Matchers.define :have_many do |association|
+ match do |object|
+ object = object.class if object.is_a? ActiveRecord::Base
+ object.reflect_on_all_associations(:has_many).find { |a| a.name == association }
+ end
+end
- def have_valid_associations
- HaveValidAssociationMatcher.new
- end
- end
+RSpec::Matchers.define :have_one do |association|
+ match do |object|
+ object = object.class if object.is_a? ActiveRecord::Base
+ object.reflect_on_all_associations(:has_one).find { |a| a.name == association }
end
end
+
+RSpec::Matchers.define :have_and_belong_to_many do |association|
+ match do |object|
+ object = object.class if object.is_a? ActiveRecord::Base
+ object.reflect_on_all_associations(:has_and_belongs_to_many).find { |a| a.name == association }
+ end
+end
View
34 lib/spec/rails/matchers/observers.rb
@@ -1,32 +1,6 @@
-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
+RSpec::Matchers.define :observe do |observed_class|
+ match do |observing_class|
+ observed_classes = observing_class.observed_classes.flatten
+ observed_classes.include?(observed_classes)
end
end
View
142 lib/spec/rails/matchers/validations.rb
@@ -1,92 +1,76 @@
-module Spec
- module Rails
- module Matchers
- def validate_presence_of(attribute)
- return simple_matcher("model to validate the presence of #{attribute}") do |model|
- if !model.respond_to?("#{attribute}=")
- false
- else
- model.send("#{attribute}=", nil)
- !model.valid? && model.errors.invalid?(attribute)
- end
- end
- end
-
- def validate_length_of(attribute, options)
- if options.has_key? :within
- min = options[:within].first
- max = options[:within].last
- elsif options.has_key? :is
- min = options[:is]
- max = min
- elsif options.has_key? :minimum
- min = options[:minimum]
- elsif options.has_key? :maximum
- max = options[:maximum]
- end
-
- return simple_matcher("model to validate the length of #{attribute} within #{min || 0} and #{max || 'Infinity'}") do |model|
- invalid = false
- if !min.nil? && min >= 1
- model.send("#{attribute}=", 'a' * (min - 1))
-
- invalid = !model.valid? && model.errors.invalid?(attribute)
- end
-
- if !max.nil?
- model.send("#{attribute}=", 'a' * (max + 1))
+RSpec::Matchers.define :validate_presence_of do |attribute|
+ match do |object|
+ if !object.respond_to?("#{attribute}=")
+ false
+ else
+ object.send("#{attribute}=", nil)
+ !object.valid? && object.errors[attribute].any?
+ end
+ end
+end
- invalid ||= !model.valid? && model.errors.invalid?(attribute)
- end
- invalid
- end
- end
+RSpec::Matchers.define :validate_length_of do |attribute, options|
+ if options.has_key? :within
+ min = options[:within].first
+ max = options[:within].last
+ elsif options.has_key? :is
+ min = options[:is]
+ max = min
+ elsif options.has_key? :minimum
+ min = options[:minimum]
+ elsif options.has_key? :maximum
+ max = options[:maximum]
+ end
- def validate_uniqueness_of(attribute)
- return simple_matcher("model to validate the uniqueness of #{attribute}") do |model|
- model.class.stub!(:with_exclusive_scope).and_return([model])
- !model.valid? && model.errors.invalid?(attribute)
- end
- end
+ invalid = false
+ if !min.nil? && min >= 1
+ object.send("#{attribute}=", 'a' * (min - 1))
+ invalid = !object.valid? && object.errors[attribute].any?
+ end
- def validate_confirmation_of(attribute)
- return simple_matcher("model to validate the confirmation of #{attribute}") do |model|
- model.send("#{attribute}_confirmation=", 'asdf')
- !model.valid? && model.errors.invalid?(attribute)
- end
- end
+ if !max.nil?
+ object.send("#{attribute}=", 'a' * (max + 1))
+ invalid ||= !object.valid? && object.errors[attribute].any?
+ end
- def validate_inclusion_of(attribute, hash)
- values = hash[:in]
+ invalid
+end
- simple_matcher("model to validate the inclusion of #{attribute} in #{values.inspect}") do |model|
- booleans = assign_and_validate_values(model, attribute, values)
- booleans.all?
- end
- end
+RSpec::Matchers.define :validate_uniqueness_of do |attribute|
+ match do |object|
+ object.class.stub!(:with_exclusive_scope).and_return([object])
+ !object.valid? && object.errors[attribute].any?
+ end
+end
- def validate_boolean_of(attribute)
- values = [true, false]
+RSpec::Matchers.define :validate_confirmation_of do |attribute|
+ match do |object|
+ object.send("#{attribute}_confirmation=", 'asdf')
+ !object.valid? && object.errors[attribute].any?
+ end
+end
- simple_matcher("model to validate boolean of #{attribute}") do |model|
- booleans = assign_and_validate_values(model, attribute, [true, false])
- booleans << !assign_and_validate_value(model, attribute, nil)
- booleans.all?
- end
- end
+RSpec::Matchers.define :validate_inclusion_of do |attribute, options|
+ match do |object|
+ values = options[:in]
+ booleans = assign_and_validate_values(object, attribute, values)
+ booleans.all?
+ end
+end
- private
+module RSpec
+ module Matchers
- def assign_and_validate_value(model, attribute, value)
- model.send("#{attribute}=", value)
- model.valid?
- !model.errors.invalid?(attribute)
- end
+ private
+ def assign_and_validate_value(model, attribute, value)
+ model.send("#{attribute}=", value)
+ model.valid?
+ !model.errors.invalid?(attribute)
+ end
- def assign_and_validate_values(model, attribute, values)
- values.map do |value|
- assign_and_validate_value(model, attribute, value)
- end
+ def assign_and_validate_values(model, attribute, values)
+ values.map do |value|
+ assign_and_validate_value(model, attribute, value)
end
end
end
View
164 lib/spec/rails/matchers/views.rb
@@ -1,102 +1,96 @@
-module Spec
- module Rails
- module Matchers
- def have_form_posting_to(url_or_path)
- return simple_matcher("have a form submitting via POST to '#{url_or_path}'") do |response|
- have_tag("form[method=post][action=#{url_or_path}]").matches?(response)
- end
- end
+RSpec::Matchers.define :have_form_posting_to do |url_or_path|
+ match do |response|
+ have_tag("form[method=post][action=#{url_or_path}]").matches?(response)
+ end
+end
- def have_form_puting_to(url_or_path, id)
- return simple_matcher("have a form submitting via PUT to '#{url_or_path}/#{id}'") do |response|
- have_tag("form[method=post][action=#{url_or_path}/#{id}]").matches?(response)
- have_tag("input[name=_method][type=hidden][value=put]").matches?(response)
- end
- end
+RSpec::Matchers.define :have_form_puting_to do |url_or_path|
+ match do |response|
+ have_tag("form[method=post][action=#{url_or_path}/#{id}]").matches?(response)
+ have_tag("input[name=_method][type=hidden][value=put]").matches?(response)
+ end
+end
- def have_label_for(attribute, text)
- return simple_matcher("have a label for '#{attribute}' with value of '#{text}'") do |response|
- have_tag("label[for=#{attribute}]").matches?(response)
- end
- end
+RSpec::Matchers.define :have_label_for do |attribute|
+ match do |response|
+ have_tag("label[for=#{attribute}]").matches?(response)
+ end
+end
- def with_label_for(attribute, text)
- return simple_matcher("have a label for '#{attribute}' with value of '#{text}'") do |response|
- with_tag("label[for=#{attribute}]").matches?(response)
- end
- end
+RSpec::Matchers.define :with_label_for do |attribute|
+ match do |response|
+ with_tag("label[for=#{attribute}]").matches?(response)
+ end
+end
- def have_text_field_for(attribute)
- return simple_matcher("have a text field for '#{attribute}'") do |response|
- have_tag("input##{attribute}[type=text]").matches?(response)
- end
- end
+RSpec::Matchers.define :have_text_field_for do |attribute|
+ match do |response|
+ have_tag("input##{attribute}[type=text]").matches?(response)
+ end
+end
- def with_text_field_for(attribute)
- return simple_matcher("with a text field for '#{attribute}'") do |response|
- with_tag("input##{attribute}[type=text]").matches?(response)
- end
- end
+RSpec::Matchers.define :with_text_field_for do |attribute|
+ match do |response|
+ with_tag("input##{attribute}[type=text]").matches?(response)
+ end
+end
- def have_text_area_for(attribute)
- return simple_matcher("have a text field for '#{attribute}'") do |response|
- have_tag("textarea##{attribute}[type=text]").matches?(response)
- end
- end
+RSpec::Matchers.define :have_text_area_for do |attribute|
+ match do |response|
+ have_tag("textarea##{attribute}[type=text]").matches?(response)
+ end
+end
- def with_text_area_for(attribute)
- return simple_matcher("have a text field for '#{attribute}'") do |response|
- with_tag("textarea##{attribute}[type=text]").matches?(response)
- end
- end
+RSpec::Matchers.define :with_text_area_for do |attribute|
+ match do |response|
+ with_tag("textarea##{attribute}[type=text]").matches?(response)
+ end
+end
- def have_password_field_for(attribute)
- return simple_matcher("have a password field for '#{attribute}'") do |response|
- have_tag("input##{attribute}[type=password]").matches?(response)
- end
- end
+RSpec::Matchers.define :have_password_field_for do |attribute|
+ match do |response|
+ have_tag("input##{attribute}[type=password]").matches?(response)
+ end
+end
- def with_password_field_for(attribute)
- return simple_matcher("have a password field for '#{attribute}'") do |response|
- with_tag("input##{attribute}[type=password]").matches?(response)
- end
- end
+RSpec::Matchers.define :with_password_field_for do |attribute|
+ match do |response|
+ with_tag("input##{attribute}[type=password]").matches?(response)
+ end
+end
- def have_checkbox_for(attribute)
- return simple_matcher("have a checkbox for '#{attribute}'") do |response|
- have_tag("input##{attribute}[type=checkbox]").matches?(response)
- end
- end
+RSpec::Matchers.define :have_checkbox_for do |attribute|
+ match do |response|
+ have_tag("input##{attribute}[type=checkbox]").matches?(response)
+ end
+end
- def with_checkbox_for(attribute)
- return simple_matcher("have a checkbox for '#{attribute}'") do |response|
- with_tag("input##{attribute}[type=checkbox]").matches?(response)
- end
- end
+RSpec::Matchers.define :with_checkbox_for do |attribute|
+ match do |response|
+ with_tag("input##{attribute}[type=checkbox]").matches?(response)
+ end
+end
- def have_submit_button
- return simple_matcher("have a submit button") do |response|
- have_tag("input[type=submit]").matches?(response)
- end
- end
+RSpec::Matchers.define :have_submit_button do |attribute|
+ match do |response|
+ have_tag("input[type=submit]").matches?(response)
+ end
+end
- def with_submit_button
- return simple_matcher("have a submit button") do |response|
- with_tag("input[type=submit]").matches?(response)
- end
- end
+RSpec::Matchers.define :with_submit_button do |attribute|
+ match do |response|
+ with_tag("input[type=submit]").matches?(response)
+ end
+end
- def have_link_to(url_or_path, text = nil)
- return simple_matcher("have a link to '#{url_or_path}'") do |response|
- have_tag("a[href=#{url_or_path}]", text).matches?(response)
- end
- end
+RSpec::Matchers.define :have_link_to do |url_or_path|
+ match do |response|
+ have_tag("a[href=#{url_or_path}]", text).matches?(response)
+ end
+end
- def with_link_to(url_or_path, text = nil)
- return simple_matcher("have a link to '#{url_or_path}'") do |response|
- with_tag("a[href=#{url_or_path}]", text).matches?(response)
- end
- end
- end
+RSpec::Matchers.define :with_link_to do |url_or_path|
+ match do |response|
+ with_tag("a[href=#{url_or_path}]", text).matches?(response)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.