Skip to content

Instantly share code, notes, and snippets.

@qrush
Forked from technicalpickles/gist:131013
Created June 17, 2009 01:03
Show Gist options
  • Save qrush/131014 to your computer and use it in GitHub Desktop.
Save qrush/131014 to your computer and use it in GitHub Desktop.
diff --git a/.gitignore b/.gitignore
index 5c3fc13..563a3bf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,5 +3,3 @@ doc
coverage
.svn/
pkg
-*.swp
-tags
diff --git a/lib/shoulda.rb b/lib/shoulda.rb
index 4b46206..5e8ba21 100644
--- a/lib/shoulda.rb
+++ b/lib/shoulda.rb
@@ -1,5 +1,17 @@
module Shoulda
VERSION = "2.10.1"
+
+ load 'test/unit/assertion_failed_error' rescue nil
+ load 'mini_test/assertion' rescue nil
+
+
+ if defined?(Test::Unit::AssertionFailedError)
+ AssertionFailedError = Test::Unit::AssertionFailedError
+ elsif defined?(MiniTest::Assertion)
+ AssertionFailedError = MiniTest::Assertion
+ else
+ raise "Aieeeeee"
+ end
end
if defined? Spec
diff --git a/lib/shoulda/action_controller.rb b/lib/shoulda/action_controller.rb
index 5a48a75..4719851 100644
--- a/lib/shoulda/action_controller.rb
+++ b/lib/shoulda/action_controller.rb
@@ -1,4 +1,5 @@
require 'shoulda'
+require 'shoulda/action_controller/helpers'
require 'shoulda/action_controller/matchers'
require 'shoulda/action_controller/macros'
@@ -6,6 +7,7 @@ module Test # :nodoc: all
module Unit
class TestCase
include Shoulda::ActionController::Matchers
+ include Shoulda::ActionController::Helpers
extend Shoulda::ActionController::Macros
end
end
diff --git a/lib/shoulda/action_controller/helpers.rb b/lib/shoulda/action_controller/helpers.rb
new file mode 100644
index 0000000..654bf7b
--- /dev/null
+++ b/lib/shoulda/action_controller/helpers.rb
@@ -0,0 +1,47 @@
+module Shoulda # :nodoc:
+ module ActionController # :nodoc:
+ module Helpers # :nodoc:
+ private # :enddoc:
+
+ SPECIAL_INSTANCE_VARIABLES = %w{
+ _cookies
+ _flash
+ _headers
+ _params
+ _request
+ _response
+ _session
+ action_name
+ before_filter_chain_aborted
+ cookies
+ flash
+ headers
+ ignore_missing_templates
+ logger
+ params
+ request
+ request_origin
+ response
+ session
+ template
+ template_class
+ template_root
+ url
+ variables_added
+ }.map(&:to_s)
+
+ def instantiate_variables_from_assigns(*names, &blk)
+ old = {}
+ names = (@response.template.assigns.keys - SPECIAL_INSTANCE_VARIABLES) if names.empty?
+ names.each do |name|
+ old[name] = instance_variable_get("@#{name}")
+ instance_variable_set("@#{name}", assigns(name.to_sym))
+ end
+ blk.call
+ names.each do |name|
+ instance_variable_set("@#{name}", old[name])
+ end
+ end
+ end
+ end
+end
diff --git a/lib/shoulda/action_controller/macros.rb b/lib/shoulda/action_controller/macros.rb
index e9eb6dd..3e2834a 100644
--- a/lib/shoulda/action_controller/macros.rb
+++ b/lib/shoulda/action_controller/macros.rb
@@ -24,35 +24,31 @@ module Shoulda # :nodoc:
module Macros
include Matchers
- # Macro that creates a test asserting that the flash contains the given
- # value. Expects a +String+ or +Regexp+.
- #
- # If the argument is +nil+, it will assert that the flash is not set.
- # This behavior is deprecated.
+ # Macro that creates a test asserting that the flash contains the given value.
+ # val can be a String, a Regex, or nil (indicating that the flash should not be set)
#
# Example:
#
# should_set_the_flash_to "Thank you for placing this order."
# should_set_the_flash_to /created/i
+ # should_set_the_flash_to nil
def should_set_the_flash_to(val)
+ matcher = set_the_flash.to(val)
if val
- matcher = set_the_flash.to(val)
should matcher.description do
assert_accepts matcher, @controller
end
else
- warn "[DEPRECATION] should_set_the_flash_to nil is deprecated. " <<
- "Use should_not_set_the_flash instead."
- should_not_set_the_flash
+ should "not #{matcher.description}" do
+ assert_rejects matcher, @controller
+ end
end
end
- # Macro that creates a test asserting that the flash is empty.
+ # Macro that creates a test asserting that the flash is empty. Same as
+ # @should_set_the_flash_to nil@
def should_not_set_the_flash
- matcher = set_the_flash
- should "not #{matcher.description}" do
- assert_rejects matcher, @controller
- end
+ should_set_the_flash_to nil
end
# Macro that creates a test asserting that filter_parameter_logging
@@ -75,9 +71,8 @@ module Shoulda # :nodoc:
#
# Options:
# * <tt>:class</tt> - The expected class of the instance variable being checked.
- #
- # If a block is passed, the assigned variable is expected to be equal to
- # the return value of that block.
+ # * <tt>:equals</tt> - A string which is evaluated and compared for equality with
+ # the instance variable being checked.
#
# Example:
#
@@ -85,11 +80,25 @@ module Shoulda # :nodoc:
# should_assign_to :user, :class => User
# should_assign_to(:user) { @user }
def should_assign_to(*names, &block)
- klass = get_options!(names, :class)
+ opts = names.extract_options!
+ if opts[:equals]
+ warn "[DEPRECATION] should_assign_to :var, :equals => 'val' " <<
+ "is deprecated. Use should_assign_to(:var) { 'val' } instead."
+ end
names.each do |name|
- matcher = assign_to(name).with_kind_of(klass)
- should matcher.description do
- if block
+ matcher = assign_to(name).with_kind_of(opts[:class])
+ test_name = matcher.description
+ test_name << " which is equal to #{opts[:equals]}" if opts[:equals]
+ should test_name do
+ if opts[:equals]
+ instantiate_variables_from_assigns do
+ expected_value = eval(opts[:equals],
+ self.send(:binding),
+ __FILE__,
+ __LINE__)
+ matcher = matcher.with(expected_value)
+ end
+ elsif block
expected_value = instance_eval(&block)
matcher = matcher.with(expected_value)
end
@@ -132,29 +141,49 @@ module Shoulda # :nodoc:
# should_respond_with_content_type :rss
# should_respond_with_content_type /rss/
def should_respond_with_content_type(content_type)
- matcher = respond_with_content_type(content_type)
- should matcher.description do
+ should "respond with content type of #{content_type}" do
+ matcher = respond_with_content_type(content_type)
assert_accepts matcher, @controller
end
end
- # Macro that creates a test asserting that a value returned from the
- # session is correct. Expects the session key as a parameter, and a block
- # that returns the expected value.
- #
+ # Macro that creates a test asserting that a value returned from the session is correct.
+ # The given string is evaled to produce the resulting redirect path. All of the instance variables
+ # set by the controller are available to the evaled string.
# Example:
#
- # should_set_session(:user_id) { @user.id }
+ # should_set_session(:user_id) { '@user.id' }
# should_set_session(:message) { "Free stuff" }
- def should_set_session(key, &block)
+ def should_set_session(key, expected = nil, &block)
matcher = set_session(key)
+ if expected
+ warn "[DEPRECATION] should_set_session :key, 'val' is deprecated. " <<
+ "Use should_set_session(:key) { 'val' } instead."
+ end
should matcher.description do
- expected_value = instance_eval(&block)
- matcher = matcher.to(expected_value)
+ if expected
+ instantiate_variables_from_assigns do
+ expected_value = eval(expected,
+ self.send(:binding),
+ __FILE__,
+ __LINE__)
+ matcher = matcher.to(expected_value)
+ end
+ else
+ expected_value = instance_eval(&block)
+ matcher = matcher.to(expected_value)
+ end
assert_accepts matcher, @controller
end
end
+ # Deprecated. See should_set_session
+ def should_return_from_session(key, expected)
+ warn "[DEPRECATION] should_return_from_session is deprecated. " <<
+ "Use should_set_session instead."
+ should_set_session(key, expected)
+ end
+
# Macro that creates a test asserting that the controller rendered the given template.
# Example:
#
@@ -188,18 +217,26 @@ module Shoulda # :nodoc:
should_render_with_layout nil
end
- # Macro that creates a test asserting that the controller returned a
- # redirect to the given path. The passed description will be used when
- # generating a test name. Expects a block that returns the expected path
- # for the redirect.
- #
+ # Macro that creates a test asserting that the controller returned a redirect to the given path.
+ # The given string is evaled to produce the resulting redirect path. All of the instance variables
+ # set by the controller are available to the evaled string.
# Example:
#
# should_redirect_to("the user's profile") { user_url(@user) }
def should_redirect_to(description, &block)
+ unless block
+ warn "[DEPRECATION] should_redirect_to without a block is " <<
+ "deprecated. Use should_redirect_to('somewhere') { } instead."
+ end
should "redirect to #{description}" do
- expected_url = instance_eval(&block)
- assert_redirected_to expected_url
+ if block
+ url = instance_eval(&block)
+ else
+ instantiate_variables_from_assigns do
+ url = eval(description, self.send(:binding), __FILE__, __LINE__)
+ end
+ end
+ assert_redirected_to url
end
end
diff --git a/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb b/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
index c5f2d71..c47dba3 100644
--- a/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
+++ b/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
@@ -30,10 +30,6 @@ module Shoulda # :nodoc:
content_type
end
end
-
- def description
- "respond with content type of #{@content_type}"
- end
def matches?(controller)
@controller = controller
diff --git a/lib/shoulda/action_controller/matchers/set_session_matcher.rb b/lib/shoulda/action_controller/matchers/set_session_matcher.rb
index dc65f31..c1a823b 100644
--- a/lib/shoulda/action_controller/matchers/set_session_matcher.rb
+++ b/lib/shoulda/action_controller/matchers/set_session_matcher.rb
@@ -46,7 +46,7 @@ module Shoulda # :nodoc:
private
def assigned_value?
- !assigned_value.nil?
+ !assigned_value.blank?
end
def cleared_value?
diff --git a/lib/shoulda/action_view/macros.rb b/lib/shoulda/action_view/macros.rb
index 9a7b481..7157bd0 100644
--- a/lib/shoulda/action_view/macros.rb
+++ b/lib/shoulda/action_view/macros.rb
@@ -11,6 +11,7 @@ module Shoulda # :nodoc:
# get :new
# end
#
+ # should_render_a_form
# should_render_page_with_metadata :title => /index/
#
# should "do something else really cool" do
@@ -22,17 +23,12 @@ module Shoulda # :nodoc:
module Macros
# Macro that creates a test asserting that the rendered view contains a <form> element.
- #
- # Deprecated.
def should_render_a_form
- warn "[DEPRECATION] should_render_a_form is deprecated."
should "display a form" do
assert_select "form", true, "The template doesn't contain a <form> element"
end
end
- # Deprecated.
- #
# Macro that creates a test asserting that the rendered view contains the selected metatags.
# Values can be string or Regexps.
# Example:
@@ -44,7 +40,6 @@ module Shoulda # :nodoc:
# Example:
# should_render_page_with_metadata :title => /index/
def should_render_page_with_metadata(options)
- warn "[DEPRECATION] should_render_page_with_metadata is deprecated."
options.each do |key, value|
should "have metatag #{key}" do
if key.to_sym == :title
diff --git a/lib/shoulda/active_record/assertions.rb b/lib/shoulda/active_record/assertions.rb
index 2e4ba5a..9730fe1 100644
--- a/lib/shoulda/active_record/assertions.rb
+++ b/lib/shoulda/active_record/assertions.rb
@@ -30,8 +30,8 @@ module Shoulda # :nodoc:
#
# assert_good_value(User, :email, "user@example.com")
#
- # product = Product.new(:tangible => false)
- # assert_good_value(product, :price, "0")
+ # @product = Product.new(:tangible => false)
+ # assert_good_value(Product, :price, "0")
def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = nil)
object = get_instance_of(object_or_klass)
matcher = allow_value(value).
@@ -54,8 +54,8 @@ module Shoulda # :nodoc:
#
# assert_bad_value(User, :email, "invalid")
#
- # product = Product.new(:tangible => true)
- # assert_bad_value(product, :price, "0")
+ # @product = Product.new(:tangible => true)
+ # assert_bad_value(Product, :price, "0")
def assert_bad_value(object_or_klass, attribute, value,
error_message_to_expect = nil)
object = get_instance_of(object_or_klass)
diff --git a/lib/shoulda/active_record/helpers.rb b/lib/shoulda/active_record/helpers.rb
index 46dd926..26799c1 100644
--- a/lib/shoulda/active_record/helpers.rb
+++ b/lib/shoulda/active_record/helpers.rb
@@ -8,6 +8,19 @@ module Shoulda # :nodoc:
end
end
+ def get_instance_of(object_or_klass)
+ if object_or_klass.is_a?(Class)
+ klass = object_or_klass
+ instance_variable_get("@#{instance_variable_name_for(klass)}") || klass.new
+ else
+ object_or_klass
+ end
+ end
+
+ def instance_variable_name_for(klass)
+ klass.to_s.split('::').last.underscore
+ end
+
# Helper method that determines the default error message used by Active
# Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly
# introduced I18n module used for localization.
diff --git a/lib/shoulda/active_record/macros.rb b/lib/shoulda/active_record/macros.rb
index 633b6b5..d880944 100644
--- a/lib/shoulda/active_record/macros.rb
+++ b/lib/shoulda/active_record/macros.rb
@@ -4,7 +4,7 @@ module Shoulda # :nodoc:
#
# These helpers will test most of the validations and associations for your ActiveRecord models.
#
- # class UserTest < Test::Unit::TestCase
+ # class UserTest < ActiveSupport::TestCase
# should_validate_presence_of :name, :phone_number
# should_not_allow_values_for :phone_number, "abcd", "1234"
# should_allow_values_for :phone_number, "(123) 456-7890"
@@ -25,6 +25,10 @@ module Shoulda # :nodoc:
# Ensures that the model cannot be saved if one of the attributes listed is not present.
#
+ # If an instance variable has been created in the setup named after the
+ # model being tested, then this method will use that. Otherwise, it will
+ # create a new instance to test against.
+ #
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.blank')</tt>
@@ -34,15 +38,23 @@ module Shoulda # :nodoc:
#
def should_validate_presence_of(*attributes)
message = get_options!(attributes, :message)
+ klass = model_class
attributes.each do |attribute|
matcher = validate_presence_of(attribute).with_message(message)
should matcher.description do
- assert_accepts(matcher, subject)
+ assert_accepts(matcher, get_instance_of(klass))
end
end
end
+ # Deprecated. See should_validate_presence_of
+ def should_require_attributes(*attributes)
+ warn "[DEPRECATION] should_require_attributes is deprecated. " <<
+ "Use should_validate_presence_of instead."
+ should_validate_presence_of(*attributes)
+ end
+
# Ensures that the model cannot be saved if one of the attributes listed is not unique.
# Requires an existing record
#
@@ -66,27 +78,37 @@ module Shoulda # :nodoc:
scope = [*scope].compact
case_sensitive = true if case_sensitive.nil?
+ klass = model_class
+
attributes.each do |attribute|
matcher = validate_uniqueness_of(attribute).
with_message(message).scoped_to(scope)
matcher = matcher.case_insensitive unless case_sensitive
should matcher.description do
- assert_accepts(matcher, subject)
+ assert_accepts(matcher, get_instance_of(klass))
end
end
end
+ # Deprecated. See should_validate_uniqueness_of
+ def should_require_unique_attributes(*attributes)
+ warn "[DEPRECATION] should_require_unique_attributes is deprecated. " <<
+ "Use should_validate_uniqueness_of instead."
+ should_validate_uniqueness_of(*attributes)
+ end
+
# Ensures that the attribute can be set on mass update.
#
# should_allow_mass_assignment_of :first_name, :last_name
#
def should_allow_mass_assignment_of(*attributes)
get_options!(attributes)
+ klass = model_class
attributes.each do |attribute|
matcher = allow_mass_assignment_of(attribute)
should matcher.description do
- assert_accepts matcher, subject
+ assert_accepts matcher, klass.new
end
end
end
@@ -97,32 +119,45 @@ module Shoulda # :nodoc:
#
def should_not_allow_mass_assignment_of(*attributes)
get_options!(attributes)
+ klass = model_class
attributes.each do |attribute|
matcher = allow_mass_assignment_of(attribute)
should "not #{matcher.description}" do
- assert_rejects matcher, subject
+ assert_rejects matcher, klass.new
end
end
end
+ # Deprecated. See should_not_allow_mass_assignment_of
+ def should_protect_attributes(*attributes)
+ warn "[DEPRECATION] should_protect_attributes is deprecated. " <<
+ "Use should_not_allow_mass_assignment_of instead."
+ should_not_allow_mass_assignment_of(*attributes)
+ end
+
# Ensures that the attribute cannot be changed once the record has been created.
#
# should_have_readonly_attributes :password, :admin_flag
#
def should_have_readonly_attributes(*attributes)
get_options!(attributes)
+ klass = model_class
attributes.each do |attribute|
matcher = have_readonly_attribute(attribute)
should matcher.description do
- assert_accepts matcher, subject
+ assert_accepts matcher, klass.new
end
end
end
# Ensures that the attribute cannot be set to the given values
#
+ # If an instance variable has been created in the setup named after the
+ # model being tested, then this method will use that. Otherwise, it will
+ # create a new instance to test against.
+ #
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.invalid')</tt>
@@ -132,31 +167,41 @@ module Shoulda # :nodoc:
#
def should_not_allow_values_for(attribute, *bad_values)
message = get_options!(bad_values, :message)
+ klass = model_class
bad_values.each do |value|
matcher = allow_value(value).for(attribute).with_message(message)
should "not #{matcher.description}" do
- assert_rejects matcher, subject
+ assert_rejects matcher, get_instance_of(klass)
end
end
end
# Ensures that the attribute can be set to the given values.
#
+ # If an instance variable has been created in the setup named after the
+ # model being tested, then this method will use that. Otherwise, it will
+ # create a new instance to test against.
+ #
# Example:
# should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
#
def should_allow_values_for(attribute, *good_values)
get_options!(good_values)
+ klass = model_class
good_values.each do |value|
matcher = allow_value(value).for(attribute)
should matcher.description do
- assert_accepts matcher, subject
+ assert_accepts matcher, get_instance_of(klass)
end
end
end
# Ensures that the length of the attribute is in the given range
#
+ # If an instance variable has been created in the setup named after the
+ # model being tested, then this method will use that. Otherwise, it will
+ # create a new instance to test against.
+ #
# Options:
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % range.first</tt>
@@ -170,6 +215,8 @@ module Shoulda # :nodoc:
short_message, long_message = get_options!([opts],
:short_message,
:long_message)
+ klass = model_class
+
matcher = ensure_length_of(attribute).
is_at_least(range.first).
with_short_message(short_message).
@@ -177,12 +224,16 @@ module Shoulda # :nodoc:
with_long_message(long_message)
should matcher.description do
- assert_accepts matcher, subject
+ assert_accepts matcher, get_instance_of(klass)
end
end
# Ensures that the length of the attribute is at least a certain length
#
+ # If an instance variable has been created in the setup named after the
+ # model being tested, then this method will use that. Otherwise, it will
+ # create a new instance to test against.
+ #
# Options:
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % min_length</tt>
@@ -192,18 +243,23 @@ module Shoulda # :nodoc:
#
def should_ensure_length_at_least(attribute, min_length, opts = {})
short_message = get_options!([opts], :short_message)
+ klass = model_class
matcher = ensure_length_of(attribute).
is_at_least(min_length).
with_short_message(short_message)
should matcher.description do
- assert_accepts matcher, subject
+ assert_accepts matcher, get_instance_of(klass)
end
end
# Ensures that the length of the attribute is exactly a certain length
#
+ # If an instance variable has been created in the setup named after the
+ # model being tested, then this method will use that. Otherwise, it will
+ # create a new instance to test against.
+ #
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.wrong_length') % length</tt>
@@ -213,17 +269,22 @@ module Shoulda # :nodoc:
#
def should_ensure_length_is(attribute, length, opts = {})
message = get_options!([opts], :message)
+ klass = model_class
matcher = ensure_length_of(attribute).
is_equal_to(length).
with_message(message)
should matcher.description do
- assert_accepts matcher, subject
+ assert_accepts matcher, get_instance_of(klass)
end
end
# Ensure that the attribute is in the range specified
#
+ # If an instance variable has been created in the setup named after the
+ # model being tested, then this method will use that. Otherwise, it will
+ # create a new instance to test against.
+ #
# Options:
# * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
@@ -238,18 +299,23 @@ module Shoulda # :nodoc:
:message,
:low_message,
:high_message)
+ klass = model_class
matcher = ensure_inclusion_of(attribute).
in_range(range).
with_message(message).
with_low_message(low_message).
with_high_message(high_message)
should matcher.description do
- assert_accepts matcher, subject
+ assert_accepts matcher, get_instance_of(klass)
end
end
# Ensure that the attribute is numeric
#
+ # If an instance variable has been created in the setup named after the
+ # model being tested, then this method will use that. Otherwise, it will
+ # create a new instance to test against.
+ #
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.not_a_number')</tt>
@@ -259,15 +325,23 @@ module Shoulda # :nodoc:
#
def should_validate_numericality_of(*attributes)
message = get_options!(attributes, :message)
+ klass = model_class
attributes.each do |attribute|
matcher = validate_numericality_of(attribute).
with_message(message)
should matcher.description do
- assert_accepts matcher, subject
+ assert_accepts matcher, get_instance_of(klass)
end
end
end
+ # Deprecated. See should_validate_numericality_of
+ def should_only_allow_numeric_values_for(*attributes)
+ warn "[DEPRECATION] should_only_allow_numeric_values_for is " <<
+ "deprecated. Use should_validate_numericality_of instead."
+ should_validate_numericality_of(*attributes)
+ end
+
# Ensures that the has_many relationship exists. Will also test that the
# associated table has the required columns. Works with polymorphic
# associations.
@@ -283,10 +357,11 @@ module Shoulda # :nodoc:
#
def should_have_many(*associations)
through, dependent = get_options!(associations, :through, :dependent)
+ klass = model_class
associations.each do |association|
matcher = have_many(association).through(through).dependent(dependent)
should matcher.description do
- assert_accepts(matcher, subject)
+ assert_accepts(matcher, klass.new)
end
end
end
@@ -302,11 +377,12 @@ module Shoulda # :nodoc:
# should_have_one :god # unless hindu
#
def should_have_one(*associations)
- dependent, through = get_options!(associations, :dependent, :through)
+ dependent = get_options!(associations, :dependent)
+ klass = model_class
associations.each do |association|
- matcher = have_one(association).dependent(dependent).through(through)
+ matcher = have_one(association).dependent(dependent)
should matcher.description do
- assert_accepts(matcher, subject)
+ assert_accepts(matcher, klass.new)
end
end
end
@@ -318,11 +394,12 @@ module Shoulda # :nodoc:
#
def should_have_and_belong_to_many(*associations)
get_options!(associations)
+ klass = model_class
associations.each do |association|
matcher = have_and_belong_to_many(association)
should matcher.description do
- assert_accepts(matcher, subject)
+ assert_accepts(matcher, klass.new)
end
end
end
@@ -333,10 +410,11 @@ module Shoulda # :nodoc:
#
def should_belong_to(*associations)
dependent = get_options!(associations, :dependent)
+ klass = model_class
associations.each do |association|
matcher = belong_to(association).dependent(dependent)
should matcher.description do
- assert_accepts(matcher, subject)
+ assert_accepts(matcher, klass.new)
end
end
end
@@ -347,7 +425,7 @@ module Shoulda # :nodoc:
#
def should_have_class_methods(*methods)
get_options!(methods)
- klass = described_type
+ klass = model_class
methods.each do |method|
should "respond to class method ##{method}" do
assert_respond_to klass, method, "#{klass.name} does not have class method #{method}"
@@ -361,7 +439,7 @@ module Shoulda # :nodoc:
#
def should_have_instance_methods(*methods)
get_options!(methods)
- klass = described_type
+ klass = model_class
methods.each do |method|
should "respond to instance method ##{method}" do
assert_respond_to klass.new, method, "#{klass.name} does not have instance method #{method}"
@@ -370,7 +448,7 @@ module Shoulda # :nodoc:
end
# Ensure that the given columns are defined on the models backing SQL table.
- # Also aliased to should_have_db_column for readability.
+ # Also aliased to should_have_index for readability.
# Takes the same options available in migrations:
# :type, :precision, :limit, :default, :null, and :scale
#
@@ -386,6 +464,7 @@ module Shoulda # :nodoc:
column_type, precision, limit, default, null, scale, sql_type =
get_options!(columns, :type, :precision, :limit,
:default, :null, :scale, :sql_type)
+ klass = model_class
columns.each do |name|
matcher = have_db_column(name).
of_type(column_type).
@@ -393,7 +472,7 @@ module Shoulda # :nodoc:
:default => default, :null => null,
:scale => scale, :sql_type => sql_type)
should matcher.description do
- assert_accepts(matcher, subject)
+ assert_accepts(matcher, klass.new)
end
end
end
@@ -401,7 +480,7 @@ module Shoulda # :nodoc:
alias_method :should_have_db_column, :should_have_db_columns
# Ensures that there are DB indices on the given columns or tuples of columns.
- # Also aliased to should_have_db_index for readability
+ # Also aliased to should_have_index for readability
#
# Options:
# * <tt>:unique</tt> - whether or not the index has a unique
@@ -412,39 +491,30 @@ module Shoulda # :nodoc:
#
# Examples:
#
- # should_have_db_indices :email, :name, [:commentable_type, :commentable_id]
- # should_have_db_index :age
- # should_have_db_index :ssn, :unique => true
+ # should_have_indices :email, :name, [:commentable_type, :commentable_id]
+ # should_have_index :age
+ # should_have_index :ssn, :unique => true
#
- def should_have_db_indices(*columns)
+ def should_have_indices(*columns)
unique = get_options!(columns, :unique)
+ klass = model_class
columns.each do |column|
- matcher = have_db_index(column).unique(unique)
+ matcher = have_index(column).unique(unique)
should matcher.description do
- assert_accepts(matcher, subject)
+ assert_accepts(matcher, klass.new)
end
end
end
- alias_method :should_have_db_index, :should_have_db_indices
-
- # Deprecated. See should_have_db_index
- def should_have_index(*args)
- warn "[DEPRECATION] should_have_index is deprecated. " <<
- "Use should_have_db_index instead."
- should_have_db_index(*args)
- end
-
- # Deprecated. See should_have_db_indices
- def should_have_indices(*args)
- warn "[DEPRECATION] should_have_indices is deprecated. " <<
- "Use should_have_db_indices instead."
- should_have_db_indices(*args)
- end
+ alias_method :should_have_index, :should_have_indices
# Ensures that the model cannot be saved if one of the attributes listed is not accepted.
#
+ # If an instance variable has been created in the setup named after the
+ # model being tested, then this method will use that. Otherwise, it will
+ # create a new instance to test against.
+ #
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.accepted')</tt>
@@ -454,17 +524,23 @@ module Shoulda # :nodoc:
#
def should_validate_acceptance_of(*attributes)
message = get_options!(attributes, :message)
+ klass = model_class
attributes.each do |attribute|
matcher = validate_acceptance_of(attribute).with_message(message)
should matcher.description do
- assert_accepts matcher, subject
+ assert_accepts matcher, get_instance_of(klass)
end
end
end
- # Deprecated.
- #
+ # Deprecated. See should_validate_uniqueness_of
+ def should_require_acceptance_of(*attributes)
+ warn "[DEPRECATION] should_require_acceptance_of is deprecated. " <<
+ "Use should_validate_acceptance_of instead."
+ should_validate_acceptance_of(*attributes)
+ end
+
# Ensures that the model has a method named scope_name that returns a NamedScope object with the
# proxy options set to the options you supply. scope_name can be either a symbol, or a method
# call which will be evaled against the model. The eval'd method call has access to all the same
@@ -501,9 +577,10 @@ module Shoulda # :nodoc:
# end
#
def should_have_named_scope(scope_call, find_options = nil)
+ klass = model_class
matcher = have_named_scope(scope_call).finding(find_options)
should matcher.description do
- assert_accepts matcher.in_context(self), subject
+ assert_accepts matcher.in_context(self), klass.new
end
end
end
diff --git a/lib/shoulda/active_record/matchers.rb b/lib/shoulda/active_record/matchers.rb
index ed22875..69b35ba 100644
--- a/lib/shoulda/active_record/matchers.rb
+++ b/lib/shoulda/active_record/matchers.rb
@@ -4,13 +4,12 @@ require 'shoulda/active_record/matchers/allow_value_matcher'
require 'shoulda/active_record/matchers/ensure_length_of_matcher'
require 'shoulda/active_record/matchers/ensure_inclusion_of_matcher'
require 'shoulda/active_record/matchers/validate_presence_of_matcher'
-require 'shoulda/active_record/matchers/validate_format_of_matcher'
require 'shoulda/active_record/matchers/validate_uniqueness_of_matcher'
require 'shoulda/active_record/matchers/validate_acceptance_of_matcher'
require 'shoulda/active_record/matchers/validate_numericality_of_matcher'
require 'shoulda/active_record/matchers/association_matcher'
require 'shoulda/active_record/matchers/have_db_column_matcher'
-require 'shoulda/active_record/matchers/have_db_index_matcher'
+require 'shoulda/active_record/matchers/have_index_matcher'
require 'shoulda/active_record/matchers/have_readonly_attribute_matcher'
require 'shoulda/active_record/matchers/allow_mass_assignment_of_matcher'
require 'shoulda/active_record/matchers/have_named_scope_matcher'
diff --git a/lib/shoulda/active_record/matchers/association_matcher.rb b/lib/shoulda/active_record/matchers/association_matcher.rb
index ddb16f7..87e0176 100644
--- a/lib/shoulda/active_record/matchers/association_matcher.rb
+++ b/lib/shoulda/active_record/matchers/association_matcher.rb
@@ -133,7 +133,7 @@ module Shoulda # :nodoc:
def through_association_exists?
if through_reflection.nil?
- @missing = "#{model_class.name} does not have any relationship to #{@through}"
+ "#{model_class.name} does not have any relationship to #{@through}"
false
else
true
@@ -142,10 +142,10 @@ module Shoulda # :nodoc:
def through_association_correct?
if @through == reflection.options[:through]
+ "Expected #{model_class.name} to have #{@name} through #{@through}, " <<
+ " but got it through #{reflection.options[:through]}"
true
else
- @missing = "Expected #{model_class.name} to have #{@name} through #{@through}, " <<
- "but got it through #{reflection.options[:through]}"
false
end
end
diff --git a/lib/shoulda/active_record/matchers/have_db_index_matcher.rb b/lib/shoulda/active_record/matchers/have_db_index_matcher.rb
deleted file mode 100644
index 3d35e2a..0000000
--- a/lib/shoulda/active_record/matchers/have_db_index_matcher.rb
+++ /dev/null
@@ -1,112 +0,0 @@
-module Shoulda # :nodoc:
- module ActiveRecord # :nodoc:
- module Matchers
-
- # Ensures that there are DB indices on the given columns or tuples of
- # columns.
- #
- # Options:
- # * <tt>unique</tt> - whether or not the index has a unique
- # constraint. Use <tt>true</tt> to explicitly test for a unique
- # constraint. Use <tt>false</tt> to explicitly test for a non-unique
- # constraint. Use <tt>nil</tt> if you don't care whether the index is
- # unique or not. Default = <tt>nil</tt>
- #
- # Examples:
- #
- # it { should have_db_index(:age) }
- # it { should have_db_index([:commentable_type, :commentable_id]) }
- # it { should have_db_index(:ssn).unique(true) }
- #
- def have_db_index(columns)
- HaveDbIndexMatcher.new(:have_index, columns)
- end
-
- class HaveDbIndexMatcher # :nodoc:
- def initialize(macro, columns)
- @macro = macro
- @columns = normalize_columns_to_array(columns)
- end
-
- def unique(unique)
- @unique = unique
- self
- end
-
- def matches?(subject)
- @subject = subject
- index_exists? && correct_unique?
- end
-
- def failure_message
- "Expected #{expectation} (#{@missing})"
- end
-
- def negative_failure_message
- "Did not expect #{expectation}"
- end
-
- def description
- "have a #{index_type} index on columns #{@columns.join(' and ')}"
- end
-
- protected
-
- def index_exists?
- ! matched_index.nil?
- end
-
- def correct_unique?
- return true if @unique.nil?
- if matched_index.unique == @unique
- true
- else
- @missing = "#{table_name} has an index named #{matched_index.name} " <<
- "of unique #{matched_index.unique}, not #{@unique}."
- false
- end
- end
-
- def matched_index
- indexes.detect { |each| each.columns == @columns }
- end
-
- def model_class
- @subject.class
- end
-
- def table_name
- model_class.table_name
- end
-
- def indexes
- ::ActiveRecord::Base.connection.indexes(table_name)
- end
-
- def expectation
- expected = "#{model_class.name} to #{description}"
- end
-
- def index_type
- case @unique
- when nil
- ''
- when false
- 'non-unique'
- else
- 'unique'
- end
- end
-
- def normalize_columns_to_array(columns)
- if columns.class == Array
- columns.collect { |each| each.to_s }
- else
- [columns.to_s]
- end
- end
- end
-
- end
- end
-end
diff --git a/lib/shoulda/active_record/matchers/have_index_matcher.rb b/lib/shoulda/active_record/matchers/have_index_matcher.rb
new file mode 100644
index 0000000..be4a8c4
--- /dev/null
+++ b/lib/shoulda/active_record/matchers/have_index_matcher.rb
@@ -0,0 +1,105 @@
+module Shoulda # :nodoc:
+ module ActiveRecord # :nodoc:
+ module Matchers
+
+ # Ensures that there are DB indices on the given columns or tuples of
+ # columns.
+ #
+ # Options:
+ # * <tt>unique</tt> - whether or not the index has a unique
+ # constraint. Use <tt>true</tt> to explicitly test for a unique
+ # constraint. Use <tt>false</tt> to explicitly test for a non-unique
+ # constraint. Use <tt>nil</tt> if you don't care whether the index is
+ # unique or not. Default = <tt>nil</tt>
+ #
+ # Examples:
+ #
+ # it { should have_index(:age) }
+ # it { should have_index([:commentable_type, :commentable_id]) }
+ # it { should have_index(:ssn).unique(true) }
+ #
+ def have_index(columns)
+ HaveIndexMatcher.new(:have_index, columns)
+ end
+
+ class HaveIndexMatcher # :nodoc:
+ def initialize(macro, columns)
+ @macro = macro
+ @columns = normalize_columns_to_array(columns)
+ end
+
+ def unique(unique)
+ @unique = unique
+ self
+ end
+
+ def matches?(subject)
+ @subject = subject
+ index_exists? && correct_unique?
+ end
+
+ def failure_message
+ "Expected #{expectation} (#{@missing})"
+ end
+
+ def negative_failure_message
+ "Did not expect #{expectation}"
+ end
+
+ def description
+ "have a #{index_type} index on columns #{@columns}"
+ end
+
+ protected
+
+ def index_exists?
+ ! matched_index.nil?
+ end
+
+ def correct_unique?
+ return true if @unique.nil?
+ if matched_index.unique == @unique
+ true
+ else
+ @missing = "#{table_name} has an index named #{matched_index.name} " <<
+ "of unique #{matched_index.unique}, not #{@unique}."
+ false
+ end
+ end
+
+ def matched_index
+ indexes.detect { |each| each.columns == @columns }
+ end
+
+ def model_class
+ @subject.class
+ end
+
+ def table_name
+ model_class.table_name
+ end
+
+ def indexes
+ ::ActiveRecord::Base.connection.indexes(table_name)
+ end
+
+ def expectation
+ expected = "#{model_class.name} to #{description}"
+ end
+
+ def index_type
+ @unique ? "unique" : "non-unique"
+ end
+
+ def normalize_columns_to_array(columns)
+ if columns.class == Array
+ columns.collect { |each| each.to_s }
+ else
+ [columns.to_s]
+ end
+ end
+ end
+
+ end
+ end
+end
diff --git a/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb b/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb
index 7ead041..576e8ce 100644
--- a/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb
+++ b/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb
@@ -2,8 +2,6 @@ module Shoulda # :nodoc:
module ActiveRecord # :nodoc:
module Matchers
- # Deprecated.
- #
# Ensures that the model has a method named scope_call that returns a
# NamedScope object with the proxy options set to the options you supply.
# scope_call can be either a symbol, or a Ruby expression in a String
@@ -45,7 +43,6 @@ module Shoulda # :nodoc:
# end
#
def have_named_scope(scope_call)
- warn "[DEPRECATION] should_have_named_scope is deprecated."
HaveNamedScopeMatcher.new(scope_call).in_context(self)
end
diff --git a/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb b/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
deleted file mode 100644
index 5605045..0000000
--- a/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb
+++ /dev/null
@@ -1,67 +0,0 @@
-module Shoulda # :nodoc:
- module ActiveRecord # :nodoc:
- module Matchers
-
- # Ensures that the model is not valid if the given attribute is not
- # formatted correctly.
- #
- # Options:
- # * <tt>with_message</tt> - value the test expects to find in
- # <tt>errors.on(:attribute)</tt>. <tt>Regexp</tt> or <tt>String</tt>.
- # Defaults to the translation for <tt>:blank</tt>.
- # * <tt>with(string to test against)</tt>
- # * <tt>not_with(string to test against)</tt>
- #
- # Examples:
- # it { should validate_format_of(:name).
- # with('12345').
- # with_message(/is not optional/) }
- # it { should validate_format_of(:name).
- # not_with('12D45').
- # with_message(/is not optional/) }
- #
- def validate_format_of(attr)
- ValidateFormatOfMatcher.new(attr)
- end
-
- class ValidateFormatOfMatcher < ValidationMatcher # :nodoc:
-
- def initialize(attribute)
- super
- end
-
- def with_message(message)
- @expected_message = message if message
- self
- end
-
- def with(value)
- raise "You may not call both with and not_with" if @value_to_fail
- @value_to_pass = value
- self
- end
-
-
- def not_with(value)
- raise "You may not call both with and not_with" if @value_to_pass
- @value_to_fail = value
- self
- end
-
-
- def matches?(subject)
- super(subject)
- @expected_message ||= :blank
- return disallows_value_of(@value_to_fail, @expected_message) if @value_to_fail
- allows_value_of(@value_to_pass, @expected_message) if @value_to_pass
- end
-
- def description
- "#{@attribute} have a valid format"
- end
-
- end
-
- end
- end
-end
diff --git a/lib/shoulda/active_record/matchers/validation_matcher.rb b/lib/shoulda/active_record/matchers/validation_matcher.rb
index dbf6d59..7faf50c 100644
--- a/lib/shoulda/active_record/matchers/validation_matcher.rb
+++ b/lib/shoulda/active_record/matchers/validation_matcher.rb
@@ -18,7 +18,6 @@ module Shoulda # :nodoc:
@subject = subject
false
end
-
private
diff --git a/lib/shoulda/assertions.rb b/lib/shoulda/assertions.rb
index 8589efa..73f31ec 100644
--- a/lib/shoulda/assertions.rb
+++ b/lib/shoulda/assertions.rb
@@ -45,27 +45,15 @@ module Shoulda # :nodoc:
end
# Asserts that the given matcher returns true when +target+ is passed to #matches?
- def assert_accepts(matcher, target, options = {})
- if matcher.matches?(target)
- assert_block { true }
- if options[:message]
- assert_match options[:message], matcher.negative_failure_message
- end
- else
- assert_block(matcher.failure_message) { false }
- end
+ def assert_accepts(matcher, target)
+ success = matcher.matches?(target)
+ assert_block(matcher.failure_message) { success }
end
# Asserts that the given matcher returns false when +target+ is passed to #matches?
- def assert_rejects(matcher, target, options = {})
- unless matcher.matches?(target)
- assert_block { true }
- if options[:message]
- assert_match options[:message], matcher.failure_message
- end
- else
- assert_block(matcher.negative_failure_message) { false }
- end
+ def assert_rejects(matcher, target)
+ success = !matcher.matches?(target)
+ assert_block(matcher.negative_failure_message) { success }
end
end
end
diff --git a/lib/shoulda/context.rb b/lib/shoulda/context.rb
index 948f18c..4743ca3 100644
--- a/lib/shoulda/context.rb
+++ b/lib/shoulda/context.rb
@@ -27,7 +27,7 @@ module Shoulda
#
# === Example:
#
- # class UserTest < Test::Unit::TestCase
+ # class UserTest < ActiveSupport::TestCase
#
# def setup
# @user = User.new("John", "Doe")
@@ -76,7 +76,7 @@ module Shoulda
#
# === Example:
#
- # class UserControllerTest < Test::Unit::TestCase
+ # class UserControllerTest < ActionController::TestCase
# context "the index action" do
# setup do
# @users = [Factory(:user)]
@@ -116,7 +116,7 @@ module Shoulda
#
# A context block can contain setup, should, should_eventually, and teardown blocks.
#
- # class UserTest < Test::Unit::TestCase
+ # class UserTest < ActiveSupport::TestCase
# context "A User instance" do
# setup do
# @user = User.find(:first)
@@ -133,7 +133,7 @@ module Shoulda
# Contexts may be nested. Nested contexts run their setup blocks from out to in before each
# should statement. They then run their teardown blocks from in to out after each should statement.
#
- # class UserTest < Test::Unit::TestCase
+ # class UserTest < ActiveSupport::TestCase
# context "A User instance" do
# setup do
# @user = User.find(:first)
@@ -170,98 +170,6 @@ module Shoulda
context.build
end
end
-
- # Returns the class being tested, as determined by the test class name.
- #
- # class UserTest; described_type; end
- # # => User
- def described_type
- self.name.gsub(/Test$/, '').constantize
- end
-
- # Sets the return value of the subject instance method:
- #
- # class UserTest < Test::Unit::TestCase
- # subject { User.first }
- #
- # # uses the existing user
- # should_validate_uniqueness_of :email
- # end
- def subject(&block)
- @subject_block = block
- end
-
- def subject_block # :nodoc:
- @subject_block
- end
- end
-
- module InstanceMethods
- # Returns an instance of the class under test.
- #
- # class UserTest
- # should "be a user" do
- # assert_kind_of User, subject # passes
- # end
- # end
- #
- # The subject can be explicitly set using the subject class method:
- #
- # class UserTest
- # subject { User.first }
- # should "be an existing user" do
- # assert !subject.new_record? # uses the first user
- # end
- # end
- #
- # If an instance variable exists named after the described class, that
- # instance variable will be used as the subject. This behavior is
- # deprecated, and will be removed in a future version of Shoulda. The
- # recommended approach for using a different subject is to use the subject
- # class method.
- #
- # class UserTest
- # should "be the existing user" do
- # @user = User.new
- # assert_equal @user, subject # passes
- # end
- # end
- #
- # The subject is used by all macros that require an instance of the class
- # being tested.
- def subject
- if subject_block
- instance_eval(&subject_block)
- else
- get_instance_of(self.class.described_type)
- end
- end
-
- def subject_block # :nodoc:
- (@shoulda_context && @shoulda_context.subject_block) || self.class.subject_block
- end
-
- def get_instance_of(object_or_klass) # :nodoc:
- if object_or_klass.is_a?(Class)
- klass = object_or_klass
- ivar = "@#{instance_variable_name_for(klass)}"
- if instance = instance_variable_get(ivar)
- warn "[WARNING] Using #{ivar} as the subject. Future versions " <<
- "of Shoulda will require an explicit subject using the " <<
- "subject class method. Add this after your setup to avoid " <<
- "this warning: subject { #{ivar} }"
- instance
- else
- klass.new
- end
- else
- object_or_klass
- end
- end
-
- def instance_variable_name_for(klass) # :nodoc:
- klass.to_s.split('::').last.underscore
- end
end
class Context # :nodoc:
@@ -273,7 +181,6 @@ module Shoulda
attr_accessor :teardown_blocks # blocks given via teardown methods
attr_accessor :shoulds # array of hashes representing the should statements
attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
- attr_accessor :subject_block
def initialize(name, parent, &blk)
Shoulda.add_context(self)
@@ -317,10 +224,6 @@ module Shoulda
self.should_eventuallys << { :name => name, :block => blk }
end
- def subject(&block)
- self.subject_block = block
- end
-
def full_name
parent_name = parent.full_name if am_subcontext?
return [parent_name, name].join(" ").strip
@@ -343,7 +246,6 @@ module Shoulda
context = self
test_unit_class.send(:define_method, test_name) do
- @shoulda_context = context
begin
context.run_parent_setup_blocks(self)
should[:before].bind(self).call if should[:before]
diff --git a/lib/shoulda/macros.rb b/lib/shoulda/macros.rb
index 71eb08a..7eefb13 100644
--- a/lib/shoulda/macros.rb
+++ b/lib/shoulda/macros.rb
@@ -65,47 +65,8 @@ module Shoulda # :nodoc:
end
end
- # Macro that creates a test asserting that a record of the given class was
- # created.
- #
- # Example:
- #
- # context "creating a post" do
- # setup { Post.create(post_attributes) }
- # should_create :post
- # end
- def should_create(class_name)
- should_change_record_count_of(class_name, 1, 'create')
- end
-
- # Macro that creates a test asserting that a record of the given class was
- # destroyed.
- #
- # Example:
- #
- # context "destroying a post" do
- # setup { Post.first.destroy }
- # should_destroy :post
- # end
- def should_destroy(class_name)
- should_change_record_count_of(class_name, -1, 'destroy')
- end
-
private
- def should_change_record_count_of(class_name, amount, action) # :nodoc:
- klass = class_name.to_s.camelize.constantize
- before = lambda do
- @_before_change_record_count = klass.count
- end
- human_name = class_name.to_s.humanize.downcase
- should "#{action} a #{human_name}", :before => before do
- assert_equal @_before_change_record_count + amount,
- klass.count,
- "Expected to #{action} a #{human_name}"
- end
- end
-
include Shoulda::Private
end
end
diff --git a/lib/shoulda/private_helpers.rb b/lib/shoulda/private_helpers.rb
index e48d457..e579957 100644
--- a/lib/shoulda/private_helpers.rb
+++ b/lib/shoulda/private_helpers.rb
@@ -9,5 +9,12 @@ module Shoulda # :nodoc:
raise ArgumentError, "Unsupported options given: #{opts.keys.join(', ')}" unless opts.keys.empty?
return *ret
end
+
+ # Returns the model class constant, as determined by the test class name.
+ #
+ # class TestUser; model_class; end => User
+ def model_class
+ self.name.gsub(/Test$/, '').constantize
+ end
end
end
diff --git a/lib/shoulda/test_unit.rb b/lib/shoulda/test_unit.rb
index 88bf35c..1caa430 100644
--- a/lib/shoulda/test_unit.rb
+++ b/lib/shoulda/test_unit.rb
@@ -9,7 +9,6 @@ require 'shoulda/rails' if defined? RAILS_ROOT
module Test # :nodoc: all
module Unit
class TestCase
- include Shoulda::InstanceMethods
extend Shoulda::ClassMethods
include Shoulda::Assertions
extend Shoulda::Macros
diff --git a/test/fail_macros.rb b/test/fail_macros.rb
index 408cdf1..879e653 100644
--- a/test/fail_macros.rb
+++ b/test/fail_macros.rb
@@ -13,7 +13,17 @@ module Shoulda
# end
def should_fail(&block)
context "should fail when trying to run:" do
- Shoulda.expected_exceptions = [Test::Unit::AssertionFailedError]
+
+ Shoulda.expected_exceptions = []
+
+ if defined?(MiniTest::Assertion)
+ Shoulda.expected_exceptions << MiniTest::Assertion
+ end
+
+ if defined?(Test::Unit::AssertionFailedError)
+ Shoulda.expected_exceptions << Test::Unit::AssertionFailedError
+ end
+
yield block
Shoulda.expected_exceptions = nil
end
diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb
index 5dbdff8..52b9083 100644
--- a/test/functional/posts_controller_test.rb
+++ b/test/functional/posts_controller_test.rb
@@ -1,9 +1,6 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'posts_controller'
-# Re-raise errors caught by the controller.
-class PostsController; def rescue_action(e) raise e end; end
-
class PostsControllerTest < ActionController::TestCase
fixtures :all
@@ -43,13 +40,15 @@ class PostsControllerTest < ActionController::TestCase
get :index, :user_id => users(:first)
end
should_respond_with :success
- should_assign_to :user, :class => User
- should_render_template :index
+ should_assign_to :user, :class => User, :equals => 'users(:first)'
should_assign_to(:user) { users(:first) }
should_fail do
should_assign_to :user, :class => Post
end
should_fail do
+ should_assign_to :user, :equals => 'posts(:first)'
+ end
+ should_fail do
should_assign_to(:user) { posts(:first) }
end
should_assign_to :posts
@@ -67,11 +66,20 @@ class PostsControllerTest < ActionController::TestCase
should_respond_with_content_type 'application/rss+xml'
should_respond_with_content_type :rss
should_respond_with_content_type /rss/
- should_set_session(:mischief) { nil }
- should_set_session(:special) { '$2 off your next purchase' }
- should_set_session(:special_user_id) { @user.id }
- should_set_session(:false_var) { false }
+ context "deprecated" do # to avoid redefining a test
+ should_return_from_session :special, "'$2 off your next purchase'"
+ end
should_fail do
+ should_return_from_session :special, "'not special'"
+ end
+ should_set_session(:mischief) { nil }
+ should_return_from_session :malarky, "nil"
+ should_set_session :special, "'$2 off your next purchase'"
+ should_set_session :special_user_id, '@user.id'
+ context "with a block" do
+ should_set_session(:special_user_id) { @user.id }
+ end
+ should_fail do # to avoid redefining a test
should_set_session(:special_user_id) { 'value' }
end
should_assign_to :user, :posts
@@ -85,17 +93,11 @@ class PostsControllerTest < ActionController::TestCase
should_render_with_layout :wide
end
should_assign_to :false_flag
- should_set_the_flash_to nil
- should_fail do
- should_set_the_flash_to /.*/
- end
end
context "on GET to #new" do
setup { get :new, :user_id => users(:first) }
should_render_without_layout
- should_not_set_the_flash
- should_render_a_form
end
context "on POST to #create" do
@@ -105,15 +107,14 @@ class PostsControllerTest < ActionController::TestCase
:body => 'blah blah blah' }
end
+ should_redirect_to 'user_post_url(@post.user, @post)'
should_redirect_to('the created post') { user_post_url(users(:first),
assigns(:post)) }
should_fail do
- should_redirect_to('elsewhere') { user_posts_url(users(:first)) }
+ should_redirect_to 'user_posts_url(@post.user)'
end
-
- should_set_the_flash_to /success/
should_fail do
- should_not_set_the_flash
+ should_redirect_to('elsewhere') { user_posts_url(users(:first)) }
end
end
end
diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb
index 470b613..1be3c37 100644
--- a/test/functional/users_controller_test.rb
+++ b/test/functional/users_controller_test.rb
@@ -1,9 +1,6 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'users_controller'
-# Re-raise errors caught by the controller.
-class UsersController; def rescue_action(e) raise e end; end
-
class UsersControllerTest < ActionController::TestCase
fixtures :all
diff --git a/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb b/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb
index 4b77734..5b12726 100644
--- a/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb
+++ b/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class AllowMassAssignmentOfMatcherTest < ActiveSupport::TestCase # :nodoc:
+class AllowMassAssignmentOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute that is blacklisted from mass-assignment" do
setup do
diff --git a/test/matchers/active_record/allow_value_matcher_test.rb b/test/matchers/active_record/allow_value_matcher_test.rb
index 40fea8f..11fcc56 100644
--- a/test/matchers/active_record/allow_value_matcher_test.rb
+++ b/test/matchers/active_record/allow_value_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class AllowValueMatcherTest < ActiveSupport::TestCase # :nodoc:
+class AllowValueMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute with a format validation" do
setup do
diff --git a/test/matchers/active_record/association_matcher_test.rb b/test/matchers/active_record/association_matcher_test.rb
index 3d7b4df..703537e 100644
--- a/test/matchers/active_record/association_matcher_test.rb
+++ b/test/matchers/active_record/association_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
+class AssociationMatcherTest < Test::Unit::TestCase # :nodoc:
context "belong_to" do
setup do
@@ -126,9 +126,7 @@ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
define_model :parent do
has_many :children
end
- assert_rejects @matcher.through(:conceptions),
- Parent.new,
- :message => /does not have any relationship to conceptions/
+ assert_rejects @matcher.through(:conceptions), Parent.new
end
should "reject an association that has the wrong :through option" do
@@ -139,12 +137,9 @@ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
end
define_model :parent do
has_many :conceptions
- has_many :relationships
has_many :children, :through => :conceptions
end
- assert_rejects @matcher.through(:relationships),
- Parent.new,
- :message => /through relationships, but got it through conceptions/
+ assert_rejects @matcher.through(:relationships), Parent.new
end
should "accept an association with a valid :dependent option" do
diff --git a/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb b/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb
index 363a5c3..bb0b45f 100644
--- a/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb
+++ b/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class EnsureInclusionOfMatcherTest < ActiveSupport::TestCase # :nodoc:
+class EnsureInclusionOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute which must be included in a range" do
setup do
diff --git a/test/matchers/active_record/ensure_length_of_matcher_test.rb b/test/matchers/active_record/ensure_length_of_matcher_test.rb
index 6f48722..a41d5f0 100644
--- a/test/matchers/active_record/ensure_length_of_matcher_test.rb
+++ b/test/matchers/active_record/ensure_length_of_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class EnsureLengthOfMatcher < ActiveSupport::TestCase # :nodoc:
+class EnsureLengthOfMatcher < Test::Unit::TestCase # :nodoc:
context "an attribute with a non-zero minimum length validation" do
setup do
diff --git a/test/matchers/active_record/have_db_column_matcher_test.rb b/test/matchers/active_record/have_db_column_matcher_test.rb
index 6592a0a..5afeeec 100644
--- a/test/matchers/active_record/have_db_column_matcher_test.rb
+++ b/test/matchers/active_record/have_db_column_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class HaveDbColumnMatcherTest < ActiveSupport::TestCase # :nodoc:
+class HaveDbColumnMatcherTest < Test::Unit::TestCase # :nodoc:
context "have_db_column" do
setup do
diff --git a/test/matchers/active_record/have_db_index_matcher_test.rb b/test/matchers/active_record/have_db_index_matcher_test.rb
deleted file mode 100644
index 3d91904..0000000
--- a/test/matchers/active_record/have_db_index_matcher_test.rb
+++ /dev/null
@@ -1,91 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-
-class HaveDbIndexMatcherTest < ActiveSupport::TestCase # :nodoc:
-
- context "have_db_index" do
- setup do
- @matcher = have_db_index(:age)
- end
-
- should "accept an existing index" do
- db_connection = create_table 'superheros' do |table|
- table.integer :age
- end
- db_connection.add_index :superheros, :age
- define_model_class 'Superhero'
- assert_accepts @matcher, Superhero.new
- end
-
- should "reject a nonexistent index" do
- define_model :superhero
- assert_rejects @matcher, Superhero.new
- end
- end
-
- context "have_db_index with unique option" do
- setup do
- @matcher = have_db_index(:ssn).unique(true)
- end
-
- should "accept an index of correct unique" do
- db_connection = create_table 'superheros' do |table|
- table.integer :ssn
- end
- db_connection.add_index :superheros, :ssn, :unique => true
- define_model_class 'Superhero'
- assert_accepts @matcher, Superhero.new
- end
-
- should "reject an index of wrong unique" do
- db_connection = create_table 'superheros' do |table|
- table.integer :ssn
- end
- db_connection.add_index :superheros, :ssn, :unique => false
- define_model_class 'Superhero'
- assert_rejects @matcher, Superhero.new
- end
- end
-
- context "have_db_index on multiple columns" do
- setup do
- @matcher = have_db_index([:geocodable_type, :geocodable_id])
- end
-
- should "accept an existing index" do
- db_connection = create_table 'geocodings' do |table|
- table.integer :geocodable_id
- table.string :geocodable_type
- end
- db_connection.add_index :geocodings, [:geocodable_type, :geocodable_id]
- define_model_class 'Geocoding'
- assert_accepts @matcher, Geocoding.new
- end
-
- should "reject a nonexistant index" do
- db_connection = create_table 'geocodings' do |table|
- table.integer :geocodable_id
- table.string :geocodable_type
- end
- define_model_class 'Geocoding'
- assert_rejects @matcher, Geocoding.new
- end
- end
-
- should "join columns with and describing multiple columns" do
- assert_match /on columns user_id and post_id/,
- have_db_index([:user_id, :post_id]).description
- end
-
- should "describe a unique index as unique" do
- assert_match /a unique index/, have_db_index(:user_id).unique(true).description
- end
-
- should "describe a non-unique index as non-unique" do
- assert_match /a non-unique index/, have_db_index(:user_id).unique(false).description
- end
-
- should "not describe an index's uniqueness when it isn't important" do
- assert_no_match /unique/, have_db_index(:user_id).description
- end
-
-end
diff --git a/test/matchers/active_record/have_index_matcher_test.rb b/test/matchers/active_record/have_index_matcher_test.rb
new file mode 100644
index 0000000..a7cea9f
--- /dev/null
+++ b/test/matchers/active_record/have_index_matcher_test.rb
@@ -0,0 +1,74 @@
+require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
+
+class HaveIndexMatcherTest < Test::Unit::TestCase # :nodoc:
+
+ context "have_index" do
+ setup do
+ @matcher = have_index(:age)
+ end
+
+ should "accept an existing index" do
+ db_connection = create_table 'superheros' do |table|
+ table.integer :age
+ end
+ db_connection.add_index :superheros, :age
+ define_model_class 'Superhero'
+ assert_accepts @matcher, Superhero.new
+ end
+
+ should "reject a nonexistent index" do
+ define_model :superhero
+ assert_rejects @matcher, Superhero.new
+ end
+ end
+
+ context "have_index with unique option" do
+ setup do
+ @matcher = have_index(:ssn).unique(true)
+ end
+
+ should "accept an index of correct unique" do
+ db_connection = create_table 'superheros' do |table|
+ table.integer :ssn
+ end
+ db_connection.add_index :superheros, :ssn, :unique => true
+ define_model_class 'Superhero'
+ assert_accepts @matcher, Superhero.new
+ end
+
+ should "reject an index of wrong unique" do
+ db_connection = create_table 'superheros' do |table|
+ table.integer :ssn
+ end
+ db_connection.add_index :superheros, :ssn, :unique => false
+ define_model_class 'Superhero'
+ assert_rejects @matcher, Superhero.new
+ end
+ end
+
+ context "have_index on multiple columns" do
+ setup do
+ @matcher = have_index([:geocodable_type, :geocodable_id])
+ end
+
+ should "accept an existing index" do
+ db_connection = create_table 'geocodings' do |table|
+ table.integer :geocodable_id
+ table.string :geocodable_type
+ end
+ db_connection.add_index :geocodings, [:geocodable_type, :geocodable_id]
+ define_model_class 'Geocoding'
+ assert_accepts @matcher, Geocoding.new
+ end
+
+ should "reject a nonexistant index" do
+ db_connection = create_table 'geocodings' do |table|
+ table.integer :geocodable_id
+ table.string :geocodable_type
+ end
+ define_model_class 'Geocoding'
+ assert_rejects @matcher, Geocoding.new
+ end
+ end
+
+end
diff --git a/test/matchers/active_record/have_named_scope_matcher_test.rb b/test/matchers/active_record/have_named_scope_matcher_test.rb
index cbf1631..2e9ecb5 100644
--- a/test/matchers/active_record/have_named_scope_matcher_test.rb
+++ b/test/matchers/active_record/have_named_scope_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class HaveNamedScopeMatcherTest < ActiveSupport::TestCase # :nodoc:
+class HaveNamedScopeMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute with a named scope" do
setup do
diff --git a/test/matchers/active_record/have_readonly_attributes_matcher_test.rb b/test/matchers/active_record/have_readonly_attributes_matcher_test.rb
index be5ece6..81a47c8 100644
--- a/test/matchers/active_record/have_readonly_attributes_matcher_test.rb
+++ b/test/matchers/active_record/have_readonly_attributes_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class HaveReadonlyAttributesMatcherTest < ActiveSupport::TestCase # :nodoc:
+class HaveReadonlyAttributesMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute that cannot be set after being saved" do
setup do
diff --git a/test/matchers/active_record/validate_acceptance_of_matcher_test.rb b/test/matchers/active_record/validate_acceptance_of_matcher_test.rb
index 44a513e..d19d5ff 100644
--- a/test/matchers/active_record/validate_acceptance_of_matcher_test.rb
+++ b/test/matchers/active_record/validate_acceptance_of_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class ValidateAcceptanceOfMatcherTest < ActiveSupport::TestCase # :nodoc:
+class ValidateAcceptanceOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "an attribute which must be accepted" do
setup do
diff --git a/test/matchers/active_record/validate_format_of_matcher_test.rb b/test/matchers/active_record/validate_format_of_matcher_test.rb
deleted file mode 100644
index 45f7914..0000000
--- a/test/matchers/active_record/validate_format_of_matcher_test.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-
-class ValidateFormatOfMatcherTest < ActiveSupport::TestCase # :nodoc:
-
-
- context "a postal code" do
- setup do
- define_model :example, :attr => :string do
- validates_format_of :attr, :with => /^\d{5}$/
- end
- @model = Example.new
- end
-
- should "be valid" do
- assert_accepts validate_format_of(:attr).with('12345'), @model
- end
-
- should "not be valid with alpha in zip" do
- assert_rejects validate_format_of(:attr).not_with('1234a'), @model, :message=>"is invalid"
- end
-
- should "not be valid with to few digits" do
- assert_rejects validate_format_of(:attr).not_with('1234'), @model, :message=>"is invalid"
- end
-
- should "not be valid with to many digits" do
- assert_rejects validate_format_of(:attr).not_with('123456'), @model, :message=>"is invalid"
- end
-
- should "raise error if you try to call both with and not_with" do
- assert_raise RuntimeError do
- validate_format_of(:attr).not_with('123456').with('12345')
- end
- end
-
- end
-
-
-end
diff --git a/test/matchers/active_record/validate_numericality_of_matcher_test.rb b/test/matchers/active_record/validate_numericality_of_matcher_test.rb
index bc2cac0..f9ab3a1 100644
--- a/test/matchers/active_record/validate_numericality_of_matcher_test.rb
+++ b/test/matchers/active_record/validate_numericality_of_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class ValidateNumericalityOfMatcherTest < ActiveSupport::TestCase # :nodoc:
+class ValidateNumericalityOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "a numeric attribute" do
setup do
diff --git a/test/matchers/active_record/validate_presence_of_matcher_test.rb b/test/matchers/active_record/validate_presence_of_matcher_test.rb
index 6589e02..f59440a 100644
--- a/test/matchers/active_record/validate_presence_of_matcher_test.rb
+++ b/test/matchers/active_record/validate_presence_of_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class ValidatePresenceOfMatcherTest < ActiveSupport::TestCase # :nodoc:
+class ValidatePresenceOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "a required attribute" do
setup do
diff --git a/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb b/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb
index 536f91f..cf7c0f9 100644
--- a/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb
+++ b/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class ValidateUniquenessOfMatcherTest < ActiveSupport::TestCase # :nodoc:
+class ValidateUniquenessOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "a unique attribute" do
setup do
diff --git a/test/matchers/controller/assign_to_matcher_test.rb b/test/matchers/controller/assign_to_matcher_test.rb
index cbae1fa..4b32724 100644
--- a/test/matchers/controller/assign_to_matcher_test.rb
+++ b/test/matchers/controller/assign_to_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class AssignToMatcherTest < ActionController::TestCase # :nodoc:
+class AssignToMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller that assigns to an instance variable" do
setup do
diff --git a/test/matchers/controller/filter_param_matcher_test.rb b/test/matchers/controller/filter_param_matcher_test.rb
index cfa0ebb..e8437af 100644
--- a/test/matchers/controller/filter_param_matcher_test.rb
+++ b/test/matchers/controller/filter_param_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class FilterParamMatcherTest < ActionController::TestCase # :nodoc:
+class FilterParamMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller that filters no parameters" do
setup do
diff --git a/test/matchers/controller/render_with_layout_matcher_test.rb b/test/matchers/controller/render_with_layout_matcher_test.rb
index 93ae2bd..e7f7e2b 100644
--- a/test/matchers/controller/render_with_layout_matcher_test.rb
+++ b/test/matchers/controller/render_with_layout_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class RenderWithLayoutMatcherTest < ActionController::TestCase # :nodoc:
+class RenderWithLayoutMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller that renders with a layout" do
setup do
diff --git a/test/matchers/controller/respond_with_content_type_matcher_test.rb b/test/matchers/controller/respond_with_content_type_matcher_test.rb
index dc9d3be..6719ff3 100644
--- a/test/matchers/controller/respond_with_content_type_matcher_test.rb
+++ b/test/matchers/controller/respond_with_content_type_matcher_test.rb
@@ -1,32 +1,27 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class RespondWithContentTypeMatcherTest < ActionController::TestCase # :nodoc:
-
+class RespondWithContentTypeMatcherTest < Test::Unit::TestCase # :nodoc:
+
context "a controller responding with content type :xml" do
setup do
@controller = build_response { render :xml => { :user => "thoughtbot" }.to_xml }
end
-
+
should "accept responding with content type :xml" do
assert_accepts respond_with_content_type(:xml), @controller
end
-
+
should "accept responding with content type 'application/xml'" do
assert_accepts respond_with_content_type('application/xml'), @controller
end
-
+
should "accept responding with content type /xml/" do
assert_accepts respond_with_content_type(/xml/), @controller
end
-
+
should "reject responding with another content type" do
assert_rejects respond_with_content_type(:json), @controller
end
end
-
- should "generate the correct test name" do
- assert_equal "respond with content type of application/xml",
- respond_with_content_type(:xml).description
- end
-
+
end
diff --git a/test/matchers/controller/respond_with_matcher_test.rb b/test/matchers/controller/respond_with_matcher_test.rb
index 59646f2..759ae16 100644
--- a/test/matchers/controller/respond_with_matcher_test.rb
+++ b/test/matchers/controller/respond_with_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class RespondWithMatcherTest < ActionController::TestCase # :nodoc:
+class RespondWithMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller responding with success" do
setup do
diff --git a/test/matchers/controller/route_matcher_test.rb b/test/matchers/controller/route_matcher_test.rb
index 9e64cd3..ede9b4d 100644
--- a/test/matchers/controller/route_matcher_test.rb
+++ b/test/matchers/controller/route_matcher_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class RouteToMatcherTest < ActionController::TestCase # :nodoc:
+class RouteToMatcherTest < Test::Unit::TestCase # :nodoc:
context "given a controller with a defined route" do
setup do
diff --git a/test/matchers/controller/set_session_matcher_test.rb b/test/matchers/controller/set_session_matcher_test.rb
index c94ecab..771bce3 100644
--- a/test/matchers/controller/set_session_matcher_test.rb
+++ b/test/matchers/controller/set_session_matcher_test.rb
@@ -1,13 +1,10 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class SetSessionMatcherTest < ActionController::TestCase # :nodoc:
+class SetSessionMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller that sets a session variable" do
setup do
- @controller = build_response do
- session[:var] = 'value'
- session[:false_var] = false
- end
+ @controller = build_response { session[:var] = 'value' }
end
should "accept assigning to that variable" do
@@ -29,10 +26,6 @@ class SetSessionMatcherTest < ActionController::TestCase # :nodoc:
should "accept assigning nil to another variable" do
assert_accepts set_session(:other).to(nil), @controller
end
-
- should "accept assigning false to that variable" do
- assert_accepts set_session(:false_var).to(false), @controller
- end
end
end
diff --git a/test/matchers/controller/set_the_flash_matcher.rb b/test/matchers/controller/set_the_flash_matcher.rb
index 7d20911..4d0a17d 100644
--- a/test/matchers/controller/set_the_flash_matcher.rb
+++ b/test/matchers/controller/set_the_flash_matcher.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
-class SetTheFlashMatcherTest < ActionController::TestCase # :nodoc:
+class SetTheFlashMatcherTest < Test::Unit::TestCase # :nodoc:
context "a controller that sets a flash message" do
setup do
diff --git a/test/model_builder.rb b/test/model_builder.rb
index a113cd4..084f1a7 100644
--- a/test/model_builder.rb
+++ b/test/model_builder.rb
@@ -1,4 +1,4 @@
-class ActiveSupport::TestCase
+class Test::Unit::TestCase
def create_table(table_name, &block)
connection = ActiveRecord::Base.connection
diff --git a/test/other/autoload_macro_test.rb b/test/other/autoload_macro_test.rb
index 70c23ed..ecfa356 100644
--- a/test/other/autoload_macro_test.rb
+++ b/test/other/autoload_macro_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
-class AutoloadMacroTest < ActiveSupport::TestCase # :nodoc:
+class AutoloadMacroTest < Test::Unit::TestCase # :nodoc:
context "The macro auto-loader" do
should "load macros from the plugins" do
assert self.class.respond_to?('plugin_macro')
diff --git a/test/other/context_test.rb b/test/other/context_test.rb
index ff384ba..352ea14 100644
--- a/test/other/context_test.rb
+++ b/test/other/context_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
-class ContextTest < ActiveSupport::TestCase # :nodoc:
+class ContextTest < Test::Unit::TestCase # :nodoc:
def self.context_macro(&blk)
context "with a subcontext made by a macro" do
@@ -142,48 +142,4 @@ class ContextTest < ActiveSupport::TestCase # :nodoc:
should_eventually "only print this statement once for a should_eventually"
end
end
-
- class ::SomeModel; end
-
- context "given a test named after a class" do
- setup do
- self.class.stubs(:name).returns("SomeModelTest")
- end
-
- should "determine the described type" do
- assert_equal SomeModel, self.class.described_type
- end
-
- should "return a new instance of the described type as the subject if none exists" do
- assert_kind_of SomeModel, subject
- end
-
- should "return an existing instance of the described type as the subject" do
- @some_model = SomeModel.new
- assert_equal @some_model, subject
- end
-
- context "with an explicit subject block" do
- setup { @expected = SomeModel.new }
- subject { @expected }
- should "return the result of the block as the subject" do
- assert_equal @expected, subject
- end
- end
- end
-end
-
-class Subject; end
-
-class SubjectTest < ActiveSupport::TestCase
-
- def setup
- @expected = Subject.new
- end
-
- subject { @expected }
-
- should "return a specified subject" do
- assert_equal @expected, subject
- end
end
diff --git a/test/other/convert_to_should_syntax_test.rb b/test/other/convert_to_should_syntax_test.rb
index 706ca6d..8b0594f 100644
--- a/test/other/convert_to_should_syntax_test.rb
+++ b/test/other/convert_to_should_syntax_test.rb
@@ -1,9 +1,9 @@
require 'test/unit'
-class ConvertToShouldSyntaxTest < ActiveSupport::TestCase # :nodoc:
+class ConvertToShouldSyntaxTest < Test::Unit::TestCase # :nodoc:
BEFORE_FIXTURE = <<-EOS
- class DummyTest < ActiveSupport::TestCase
+ class DummyTest < Test::Unit::TestCase
should "Not change this_word_with_underscores" do
end
@@ -23,7 +23,7 @@ class ConvertToShouldSyntaxTest < ActiveSupport::TestCase # :nodoc:
EOS
AFTER_FIXTURE = <<-EOS
- class DummyTest < ActiveSupport::TestCase
+ class DummyTest < Test::Unit::TestCase
should "Not change this_word_with_underscores" do
end
diff --git a/test/other/helpers_test.rb b/test/other/helpers_test.rb
index 916e0e5..34365b2 100644
--- a/test/other/helpers_test.rb
+++ b/test/other/helpers_test.rb
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
require 'action_mailer'
require 'mocha'
-class HelpersTest < ActiveSupport::TestCase # :nodoc:
+class HelpersTest < Test::Unit::TestCase # :nodoc:
context "given delivered emails" do
setup do
@@ -14,7 +14,7 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
should "have sent an email" do
assert_sent_email
- assert_raises(Test::Unit::AssertionFailedError) do
+ assert_raises(Shoulda::AssertionFailedError) do
assert_did_not_send_email
end
end
@@ -26,7 +26,7 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
end
should "not find an email that doesn't exist" do
- assert_raises(Test::Unit::AssertionFailedError) do
+ assert_raises(Shoulda::AssertionFailedError) do
assert_sent_email do |e|
e.subject =~ /whatever/
end
@@ -42,7 +42,7 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
should "not have sent an email" do
assert_did_not_send_email
- assert_raises(Test::Unit::AssertionFailedError) do
+ assert_raises(Shoulda::AssertionFailedError) do
assert_sent_email
end
end
@@ -55,7 +55,7 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
[/b/, 'abc', 3].each do |x|
should "contain #{x.inspect}" do
- assert_raises(Test::Unit::AssertionFailedError) do
+ assert_raises(Shoulda::AssertionFailedError) do
assert_does_not_contain @a, x
end
assert_contains @a, x
@@ -63,16 +63,16 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
end
should "not contain 'wtf'" do
- assert_raises(Test::Unit::AssertionFailedError) {assert_contains @a, 'wtf'}
+ assert_raises(Shoulda::AssertionFailedError) {assert_contains @a, 'wtf'}
assert_does_not_contain @a, 'wtf'
end
should "be the same as another array, ordered differently" do
assert_same_elements(@a, [3, "def", "abc"])
- assert_raises(Test::Unit::AssertionFailedError) do
+ assert_raises(Shoulda::AssertionFailedError) do
assert_same_elements(@a, [3, 3, "def", "abc"])
end
- assert_raises(Test::Unit::AssertionFailedError) do
+ assert_raises(Shoulda::AssertionFailedError) do
assert_same_elements([@a, "abc"].flatten, [3, 3, "def", "abc"])
end
end
@@ -119,13 +119,13 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
end
should "fail to validate a bad email address" do
- assert_raises Test::Unit::AssertionFailedError do
+ assert_raises Shoulda::AssertionFailedError do
assert_good_value User.new, :email, "bad"
end
end
should "fail to validate a bad SSN that is too short" do
- assert_raises Test::Unit::AssertionFailedError do
+ assert_raises Shoulda::AssertionFailedError do
assert_good_value User.new, :ssn, "x", /length/
end
end
@@ -155,13 +155,13 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
end
should "fail to invalidate a good email address" do
- assert_raises Test::Unit::AssertionFailedError do
+ assert_raises Shoulda::AssertionFailedError do
assert_bad_value User.new, :email, "good@example.com"
end
end
should "fail to invalidate a good SSN" do
- assert_raises Test::Unit::AssertionFailedError do
+ assert_raises Shoulda::AssertionFailedError do
assert_bad_value User.new, :ssn, "xxxxxxxxx", /length/
end
end
@@ -188,25 +188,15 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
:negative_failure_message => 'big time failure')
end
- should "pass when given to assert_accepts with no message expectation" do
+ should "pass when given to assert_accepts" do
assert_accepts @matcher, 'target'
end
- should "pass when given to assert_accepts with a matching message" do
- assert_accepts @matcher, 'target', :message => /big time/
- end
-
- should "fail when given to assert_accepts with non-matching message" do
- assert_raise Test::Unit::AssertionFailedError do
- assert_accepts @matcher, 'target', :message => /small time/
- end
- end
-
context "when given to assert_rejects" do
setup do
begin
assert_rejects @matcher, 'target'
- rescue Test::Unit::AssertionFailedError => @error
+ rescue Shoulda::AssertionFailedError => @error
end
end
@@ -227,25 +217,15 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
:negative_failure_message => 'bad failure message')
end
- should "pass when given to assert_rejects with no message expectation" do
+ should "pass when given to assert_rejects" do
assert_rejects @matcher, 'target'
end
- should "pass when given to assert_rejects with a matching message" do
- assert_rejects @matcher, 'target', :message => /big time/
- end
-
- should "fail when given to assert_rejects with a non-matching message" do
- assert_raise Test::Unit::AssertionFailedError do
- assert_rejects @matcher, 'target', :message => /small time/
- end
- end
-
context "when given to assert_accepts" do
setup do
begin
assert_accepts @matcher, 'target'
- rescue Test::Unit::AssertionFailedError => @error
+ rescue Shoulda::AssertionFailedError => @error
end
end
@@ -258,68 +238,4 @@ class HelpersTest < ActiveSupport::TestCase # :nodoc:
end
end
end
-
- context "given one treat exists and one post exists" do
- setup do
- Treat.create!
- Post.create!(:title => 'title', :body => 'body', :user_id => 1)
- end
-
- teardown do
- Treat.delete_all
- Post.delete_all
- end
-
- context "creating a treat" do
- setup do
- Treat.create!
- end
-
- should_create :treat
- should_fail do
- should_create :post
- end
- end
-
- context "creating a treat and a post" do
- setup do
- Treat.create!
- Post.create!(:title => 'title 2', :body => 'body', :user_id => 1)
- end
-
- should_create :treat
- should_create :post
- end
-
- context "destroying a treat" do
- setup do
- Treat.first.destroy
- end
-
- should_destroy :treat
- should_fail do
- should_destroy :post
- end
- end
-
- context "destroying a treat and a post" do
- setup do
- Treat.first.destroy
- Post.first.destroy
- end
-
- should_destroy :treat
- should_destroy :post
- end
-
- context "doing nothing" do
- should_fail do
- should_create :treat
- end
-
- should_fail do
- should_destroy :treat
- end
- end
- end
end
diff --git a/test/other/private_helpers_test.rb b/test/other/private_helpers_test.rb
index f583599..7b2564a 100644
--- a/test/other/private_helpers_test.rb
+++ b/test/other/private_helpers_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
-class PrivateHelpersTest < ActiveSupport::TestCase # :nodoc:
+class PrivateHelpersTest < Test::Unit::TestCase # :nodoc:
include Shoulda::Private
context "get_options!" do
should "remove opts from args" do
@@ -23,4 +23,12 @@ class PrivateHelpersTest < ActiveSupport::TestCase # :nodoc:
end
end
end
+
+ class ::SomeModel; end
+ context "model_class" do
+ should "sniff the class constant from the test class" do
+ self.expects(:name).returns("SomeModelTest")
+ assert_equal SomeModel, model_class
+ end
+ end
end
diff --git a/test/other/should_test.rb b/test/other/should_test.rb
index eefed6a..f32e8f1 100644
--- a/test/other/should_test.rb
+++ b/test/other/should_test.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
-class ShouldTest < ActiveSupport::TestCase # :nodoc:
+class ShouldTest < Test::Unit::TestCase # :nodoc:
should "be able to define a should statement outside of a context" do
assert true
end
@@ -177,7 +177,7 @@ class ShouldTest < ActiveSupport::TestCase # :nodoc:
end
def test_should_create_test_methods_on_build
- tu_class = ActiveSupport::TestCase
+ tu_class = Test::Unit::TestCase
context = Shoulda::Context.new("A Context", tu_class) do
should "define the test" do; end
end
@@ -187,7 +187,7 @@ class ShouldTest < ActiveSupport::TestCase # :nodoc:
end
def test_should_create_test_methods_on_build_when_subcontext
- tu_class = ActiveSupport::TestCase
+ tu_class = Test::Unit::TestCase
context = Shoulda::Context.new("A Context", tu_class) do
context "with a child" do
should "define the test" do; end
diff --git a/test/rails_root/app/controllers/application.rb b/test/rails_root/app/controllers/application.rb
new file mode 100644
index 0000000..10fa987
--- /dev/null
+++ b/test/rails_root/app/controllers/application.rb
@@ -0,0 +1,25 @@
+# Filters added to this controller apply to all controllers in the application.
+# Likewise, all the methods added will be available for all controllers.
+
+class ApplicationController < ActionController::Base
+ # Pick a unique cookie name to distinguish our session data from others'
+ session :session_key => '_rails_root_session_id'
+
+ def ensure_logged_in
+ unless session[:logged_in]
+ respond_to do |accepts|
+ accepts.html do
+ flash[:error] = 'What do you think you\'re doing?'
+ redirect_to '/'
+ end
+ accepts.xml do
+ headers["Status"] = "Unauthorized"
+ headers["WWW-Authenticate"] = %(Basic realm="Web Password")
+ render :text => "Couldn't authenticate you", :status => '401 Unauthorized'
+ end
+ end
+ return false
+ end
+ return true
+ end
+end
diff --git a/test/rails_root/app/controllers/posts_controller.rb b/test/rails_root/app/controllers/posts_controller.rb
index 44f0286..46c9212 100644
--- a/test/rails_root/app/controllers/posts_controller.rb
+++ b/test/rails_root/app/controllers/posts_controller.rb
@@ -12,7 +12,6 @@ class PostsController < ApplicationController
headers['Content-Type'] = 'application/rss+xml'
session[:special] = '$2 off your next purchase'
session[:special_user_id] = @user.id
- session[:false_var] = false
head :ok
end
end
diff --git a/test/rails_root/app/models/profile.rb b/test/rails_root/app/models/profile.rb
deleted file mode 100644
index a169d92..0000000
--- a/test/rails_root/app/models/profile.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-class Profile < ActiveRecord::Base
-end
diff --git a/test/rails_root/app/models/registration.rb b/test/rails_root/app/models/registration.rb
deleted file mode 100644
index eb8e477..0000000
--- a/test/rails_root/app/models/registration.rb
+++ /dev/null
@@ -1,2 +0,0 @@
-class Registration < ActiveRecord::Base
-end
diff --git a/test/rails_root/app/models/user.rb b/test/rails_root/app/models/user.rb
index 68fdd69..c99857c 100644
--- a/test/rails_root/app/models/user.rb
+++ b/test/rails_root/app/models/user.rb
@@ -6,8 +6,6 @@ class User < ActiveRecord::Base
has_many :friends, :through => :friendships
has_one :address, :as => :addressable, :dependent => :destroy
- has_one :registration
- has_one :profile, :through => :registration
named_scope :old, :conditions => "age > 50"
named_scope :eighteen, :conditions => { :age => 18 }
diff --git a/test/rails_root/config/boot.rb b/test/rails_root/config/boot.rb
index cd21fb9..0a51688 100644
--- a/test/rails_root/config/boot.rb
+++ b/test/rails_root/config/boot.rb
@@ -67,7 +67,7 @@ module Rails
class << self
def rubygems_version
- Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
+ Gem::RubyGemsVersion rescue nil
end
def gem_version
@@ -82,14 +82,14 @@ module Rails
def load_rubygems
require 'rubygems'
-
- unless rubygems_version >= '0.9.4'
- $stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)
+ min_version = '1.3.1'
+ unless rubygems_version >= min_version
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end
rescue LoadError
- $stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end
diff --git a/test/rails_root/config/environment.rb b/test/rails_root/config/environment.rb
index 92a1f8d..7423ad6 100644
--- a/test/rails_root/config/environment.rb
+++ b/test/rails_root/config/environment.rb
@@ -9,10 +9,7 @@ Rails::Initializer.run do |config|
config.log_level = :debug
config.cache_classes = false
config.whiny_nils = true
- config.action_controller.session = {
- :key => 'shoulda_session',
- :secret => 'ceae6058a816b1446e09ce90d8372511'
- }
+ config.action_controller.session = { :key => "_myapp_session", :secret => "0bdea3c4af2c290fa043218210a99040" }
end
# Dependencies.log_activity = true
diff --git a/test/rails_root/db/migrate/20090506203502_create_profiles.rb b/test/rails_root/db/migrate/20090506203502_create_profiles.rb
deleted file mode 100644
index 6398c66..0000000
--- a/test/rails_root/db/migrate/20090506203502_create_profiles.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-class CreateProfiles < ActiveRecord::Migration
- def self.up
- create_table :profiles do |t|
-
- t.timestamps
- end
- end
-
- def self.down
- drop_table :profiles
- end
-end
diff --git a/test/rails_root/db/migrate/20090506203536_create_registrations.rb b/test/rails_root/db/migrate/20090506203536_create_registrations.rb
deleted file mode 100644
index ab9c317..0000000
--- a/test/rails_root/db/migrate/20090506203536_create_registrations.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-class CreateRegistrations < ActiveRecord::Migration
- def self.up
- create_table :registrations do |t|
- t.integer :user_id
- t.integer :profile_id
-
- t.timestamps
- end
- end
-
- def self.down
- drop_table :registrations
- end
-end
diff --git a/test/rails_root/test/shoulda_macros/custom_macro.rb b/test/rails_root/test/shoulda_macros/custom_macro.rb
index e35d475..ffae9e8 100644
--- a/test/rails_root/test/shoulda_macros/custom_macro.rb
+++ b/test/rails_root/test/shoulda_macros/custom_macro.rb
@@ -2,5 +2,5 @@ module CustomMacro
def custom_macro
end
end
-ActiveSupport::TestCase.extend(CustomMacro)
+Test::Unit::TestCase.extend(CustomMacro)
diff --git a/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb b/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb
index 2e36d63..6c47076 100644
--- a/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb
+++ b/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb
@@ -2,5 +2,5 @@ module GemMacro
def gem_macro
end
end
-ActiveSupport::TestCase.extend(GemMacro)
+Test::Unit::TestCase.extend(GemMacro)
diff --git a/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb b/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb
index e91da16..4c3ba16 100644
--- a/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb
+++ b/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb
@@ -2,5 +2,5 @@ module PluginMacro
def plugin_macro
end
end
-ActiveSupport::TestCase.extend(PluginMacro)
+Test::Unit::TestCase.extend(PluginMacro)
diff --git a/test/rspec_test.rb b/test/rspec_test.rb
index 77e918e..dfbc0e9 100644
--- a/test/rspec_test.rb
+++ b/test/rspec_test.rb
@@ -8,7 +8,7 @@ rescue LoadError => exception
puts "RSpec integration was not tested because RSpec is not available"
else
- class RspecTest < ActiveSupport::TestCase
+ class RspecTest < Test::Unit::TestCase
SHOULDA_ROOT =
File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
diff --git a/test/test_helper.rb b/test/test_helper.rb
index ff068fe..982ddff 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -5,6 +5,8 @@ ENV['RAILS_ENV'] = 'test'
rails_root = File.dirname(__FILE__) + '/rails_root'
+require 'test/unit' # uhhh, yeah. do this for ruby 1.9
+require 'active_support/test_case'
require "#{rails_root}/config/environment.rb"
# Load the testing framework
@@ -16,10 +18,17 @@ ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
# Setup the fixtures path
-ActiveSupport::TestCase.fixture_path =
- File.join(File.dirname(__FILE__), "fixtures")
+ActiveSupport::TestCase.fixture_path = File.join(File.dirname(__FILE__), "fixtures")
class ActiveSupport::TestCase #:nodoc:
+ def create_fixtures(*table_names)
+ if block_given?
+ Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names) { yield }
+ else
+ Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names)
+ end
+ end
+
self.use_transactional_fixtures = false
self.use_instantiated_fixtures = false
end
diff --git a/test/unit/address_test.rb b/test/unit/address_test.rb
index 766941b..6e5fc05 100644
--- a/test/unit/address_test.rb
+++ b/test/unit/address_test.rb
@@ -6,5 +6,5 @@ class AddressTest < ActiveSupport::TestCase
should_belong_to :addressable
should_validate_uniqueness_of :title, :scoped_to => [:addressable_id, :addressable_type]
should_ensure_length_at_least :zip, 5
- should_validate_numericality_of :zip
+ should_only_allow_numeric_values_for :zip
end
diff --git a/test/unit/dog_test.rb b/test/unit/dog_test.rb
index 03ff41f..89be4fa 100644
--- a/test/unit/dog_test.rb
+++ b/test/unit/dog_test.rb
@@ -5,5 +5,6 @@ class Pets::DogTest < ActiveSupport::TestCase
should_belong_to :address, :dependent => :destroy
should_have_many :treats
should_have_and_belong_to_many :fleas
- should_validate_presence_of :owner_id, :treats, :fleas
+ should_require_attributes :treats, :fleas
+ should_validate_presence_of :owner_id
end
diff --git a/test/unit/flea_test.rb b/test/unit/flea_test.rb
index 0df4473..37f4b64 100644
--- a/test/unit/flea_test.rb
+++ b/test/unit/flea_test.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/../test_helper'
-class FleaTest < ActiveSupport::TestCase
+class FleaTest < Test::Unit::TestCase
should_have_and_belong_to_many :dogs
end
diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb
index 327d138..9cc4e32 100644
--- a/test/unit/post_test.rb
+++ b/test/unit/post_test.rb
@@ -8,7 +8,7 @@ class PostTest < ActiveSupport::TestCase
should_have_many :tags, :through => :taggings
should_have_many :through_tags, :through => :taggings
- should_validate_uniqueness_of :title
+ should_require_unique_attributes :title
should_validate_presence_of :body, :message => /wtf/
should_validate_presence_of :title
should_validate_numericality_of :user_id
diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb
index 87994e6..1c47f79 100644
--- a/test/unit/product_test.rb
+++ b/test/unit/product_test.rb
@@ -2,7 +2,9 @@ require File.dirname(__FILE__) + '/../test_helper'
class ProductTest < ActiveSupport::TestCase
context "An intangible product" do
- subject { Product.new(:tangible => false) }
+ setup do
+ @product = Product.new(:tangible => false)
+ end
should_validate_presence_of :title
should_not_allow_values_for :size, "22"
@@ -11,7 +13,9 @@ class ProductTest < ActiveSupport::TestCase
end
context "A tangible product" do
- subject { Product.new(:tangible => true) }
+ setup do
+ @product = Product.new(:tangible => true)
+ end
should_validate_presence_of :price
should_ensure_value_in_range :price, 1..9999
diff --git a/test/unit/tag_test.rb b/test/unit/tag_test.rb
index 057995f..fc2e1d1 100644
--- a/test/unit/tag_test.rb
+++ b/test/unit/tag_test.rb
@@ -1,12 +1,12 @@
require File.dirname(__FILE__) + '/../test_helper'
-class TagTest < ActiveSupport::TestCase
+class TagTest < Test::Unit::TestCase
should_have_many :taggings, :dependent => :destroy
should_have_many :posts
should_ensure_length_at_least :name, 2
- should_not_allow_mass_assignment_of :secret
+ should_protect_attributes :secret
should_allow_mass_assignment_of :name
should_fail do
diff --git a/test/unit/tagging_test.rb b/test/unit/tagging_test.rb
index 2572f1b..3f8c35c 100644
--- a/test/unit/tagging_test.rb
+++ b/test/unit/tagging_test.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/../test_helper'
-class TaggingTest < ActiveSupport::TestCase
+class TaggingTest < Test::Unit::TestCase
should_belong_to :post
should_belong_to :tag
end
diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb
index 52cafe4..63ffe8e 100644
--- a/test/unit/user_test.rb
+++ b/test/unit/user_test.rb
@@ -12,15 +12,15 @@ class UserTest < ActiveSupport::TestCase
should_have_one :address
should_have_one :address, :dependent => :destroy
- should_have_db_indices :email, :name
+ should_have_indices :email, :name
should_have_index :age
- should_have_db_index [:email, :name], :unique => true
- should_have_db_index :age, :unique => false
+ should_have_index [:email, :name], :unique => true
+ should_have_index :age, :unique => false
should_fail do
- should_have_db_index :phone
- should_have_db_index :email, :unique => false
- should_have_db_index :age, :unique => true
+ should_have_index :phone
+ should_have_index :email, :unique => false
+ should_have_index :age, :unique => true
end
should_have_named_scope :old, :conditions => "age > 50"
@@ -56,6 +56,7 @@ class UserTest < ActiveSupport::TestCase
should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255,
:null => true, :scale => nil
should_validate_acceptance_of :eula
+ should_require_acceptance_of :eula
should_validate_uniqueness_of :email, :scoped_to => :name, :case_sensitive => false
should_ensure_length_is :ssn, 9, :message => "Social Security Number is not the right length"
@@ -66,11 +67,4 @@ class UserTest < ActiveSupport::TestCase
should_fail do
should_not_allow_mass_assignment_of :name, :age
end
-
- should_have_one :profile, :through => :registration
-
- should_fail do
- should_have_one :profile, :through => :interview
- should_have_one :address, :through => :registration
- end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment