Browse files

merge in changes

  • Loading branch information...
2 parents 49a2f5e + c33610a commit f01e8b522cdcf0da896fabc50384c805270fae2a @mjankowski mjankowski committed Sep 15, 2008
View
3 lib/shoulda/active_record/assertions.rb
@@ -56,7 +56,8 @@ def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid
#
# @product = Product.new(:tangible => true)
# assert_bad_value(Product, :price, "0")
- def assert_bad_value(object_or_klass, attribute, value, error_message_to_expect = /invalid/)
+ def assert_bad_value(object_or_klass, attribute, value,
+ error_message_to_expect = ::ActiveRecord::Errors.default_error_messages[:invalid])
object = get_instance_of(object_or_klass)
object.send("#{attribute}=", value)
assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}"
View
47 lib/shoulda/active_record/macros.rb
@@ -36,14 +36,14 @@ def load_all_fixtures
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/blank/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:blank]</tt>
#
# Example:
# should_require_attributes :name, :phone_number
#
def should_require_attributes(*attributes)
message = get_options!(attributes, :message)
- message ||= /blank/
+ message ||= ::ActiveRecord::Errors.default_error_messages[:blank]
klass = model_class
attributes.each do |attribute|
@@ -58,7 +58,7 @@ def should_require_attributes(*attributes)
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/taken/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:taken]</tt>
# * <tt>:scoped_to</tt> - field(s) to scope the uniqueness to.
#
# Examples:
@@ -70,7 +70,7 @@ def should_require_attributes(*attributes)
def should_require_unique_attributes(*attributes)
message, scope = get_options!(attributes, :message, :scoped_to)
scope = [*scope].compact
- message ||= /taken/
+ message ||= ::ActiveRecord::Errors.default_error_messages[:taken]
klass = model_class
attributes.each do |attribute|
@@ -118,7 +118,8 @@ def should_protect_attributes(*attributes)
protected = klass.protected_attributes || []
accessible = klass.accessible_attributes || []
- assert protected.include?(attribute.to_s) || !accessible.include?(attribute.to_s),
+ assert protected.include?(attribute.to_s) ||
+ (!accessible.empty? && !accessible.include?(attribute.to_s)),
(accessible.empty? ?
"#{klass} is protecting #{protected.to_a.to_sentence}, but not #{attribute}." :
"#{klass} has made #{attribute} accessible")
@@ -155,14 +156,14 @@ def should_have_readonly_attributes(*attributes)
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/invalid/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:invalid]</tt>
#
# Example:
# should_not_allow_values_for :isbn, "bad 1", "bad 2"
#
def should_not_allow_values_for(attribute, *bad_values)
message = get_options!(bad_values, :message)
- message ||= /invalid/
+ message ||= ::ActiveRecord::Errors.default_error_messages[:invalid]
klass = model_class
bad_values.each do |v|
should "not allow #{attribute} to be set to #{v.inspect}" do
@@ -198,17 +199,17 @@ def should_allow_values_for(attribute, *good_values)
#
# Options:
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/short/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:too_short] % range.first</tt>
# * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/long/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:too_long] % range.last</tt>
#
# Example:
# should_ensure_length_in_range :password, (6..20)
#
def should_ensure_length_in_range(attribute, range, opts = {})
short_message, long_message = get_options!([opts], :short_message, :long_message)
- short_message ||= /short/
- long_message ||= /long/
+ short_message ||= ::ActiveRecord::Errors.default_error_messages[:too_short] % range.first
+ long_message ||= ::ActiveRecord::Errors.default_error_messages[:too_long] % range.last
klass = model_class
min_length = range.first
@@ -250,14 +251,14 @@ def should_ensure_length_in_range(attribute, range, opts = {})
#
# Options:
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/short/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:too_short] % min_length</tt>
#
# Example:
# should_ensure_length_at_least :name, 3
#
def should_ensure_length_at_least(attribute, min_length, opts = {})
short_message = get_options!([opts], :short_message)
- short_message ||= /short/
+ short_message ||= ::ActiveRecord::Errors.default_error_messages[:too_short] % min_length
klass = model_class
@@ -281,14 +282,14 @@ def should_ensure_length_at_least(attribute, min_length, opts = {})
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/short/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:wrong_length] % length</tt>
#
# Example:
# should_ensure_length_is :ssn, 9
#
def should_ensure_length_is(attribute, length, opts = {})
message = get_options!([opts], :message)
- message ||= /wrong length/
+ message ||= ::ActiveRecord::Errors.default_error_messages[:wrong_length] % length
klass = model_class
@@ -316,17 +317,17 @@ def should_ensure_length_is(attribute, length, opts = {})
#
# Options:
# * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/included/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:inclusion]</tt>
# * <tt>:high_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/included/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:inclusion]</tt>
#
# Example:
# should_ensure_value_in_range :age, (0..100)
#
def should_ensure_value_in_range(attribute, range, opts = {})
low_message, high_message = get_options!([opts], :low_message, :high_message)
- low_message ||= /included/
- high_message ||= /included/
+ low_message ||= ::ActiveRecord::Errors.default_error_messages[:inclusion]
+ high_message ||= ::ActiveRecord::Errors.default_error_messages[:inclusion]
klass = model_class
min = range.first
@@ -361,14 +362,14 @@ def should_ensure_value_in_range(attribute, range, opts = {})
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/number/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:not_a_number]</tt>
#
# Example:
# should_only_allow_numeric_values_for :age
#
def should_only_allow_numeric_values_for(*attributes)
message = get_options!(attributes, :message)
- message ||= /number/
+ message ||= ::ActiveRecord::Errors.default_error_messages[:not_a_number]
klass = model_class
attributes.each do |attribute|
attribute = attribute.to_sym
@@ -604,14 +605,14 @@ def should_have_indices(*columns)
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>/must be accepted/</tt>
+ # Regexp or string. Default = <tt>ActiveRecord::Errors.default_error_messages[:accepted]</tt>
#
# Example:
# should_require_acceptance_of :eula
#
def should_require_acceptance_of(*attributes)
message = get_options!(attributes, :message)
- message ||= /must be accepted/
+ message ||= ::ActiveRecord::Errors.default_error_messages[:accepted]
klass = model_class
attributes.each do |attribute|
View
1 lib/shoulda/controller.rb
@@ -2,7 +2,6 @@
require 'shoulda/controller/helpers'
require 'shoulda/controller/resource_options'
require 'shoulda/controller/macros'
-require 'shoulda/controller/routing'
module Test # :nodoc: all
module Unit
View
38 lib/shoulda/controller/macros.rb
@@ -196,6 +196,44 @@ def should_render_a_form
assert_select "form", true, "The template doesn't contain a <form> element"
end
end
+
+ # Macro that creates a routing test. It tries to use the given HTTP
+ # +method+ on the given +path+, and asserts that it routes to the
+ # given +options+.
+ #
+ # If you don't specify a :controller, it will try to guess the controller
+ # based on the current test.
+ #
+ # +to_param+ is called on the +options+ given.
+ #
+ # Examples:
+ #
+ # should_route :get, '/posts', :action => :index
+ # should_route :post, '/posts', :controller => :posts, :action => :create
+ # should_route :get, '/posts/1', :action => :show, :id => 1
+ # should_route :put, '/posts/1', :action => :update, :id => "1"
+ # should_route :delete, '/posts/1', :action => :destroy, :id => 1
+ # should_route :get, '/posts/new', :action => :new
+ #
+ def should_route(method, path, options)
+ unless options[:controller]
+ options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
+ end
+ options[:controller] = options[:controller].to_s
+ options[:action] = options[:action].to_s
+
+ populated_path = path.dup
+ options.each do |key, value|
+ options[key] = value.to_param if value.respond_to? :to_param
+ populated_path.gsub!(key.inspect, value.to_s)
+ end
+
+ should_name = "route #{method.to_s.upcase} #{populated_path} to/from #{options.inspect}"
+
+ should should_name do
+ assert_routing({:method => method, :path => populated_path}, options)
+ end
+ end
end
end
end
View
10 lib/shoulda/controller/routing.rb
@@ -1,10 +0,0 @@
-require 'shoulda'
-require 'shoulda/controller/routing/macros'
-
-module Test # :nodoc: all
- module Unit
- class TestCase
- extend ThoughtBot::Shoulda::Controller::Routing::Macros
- end
- end
-end
View
47 lib/shoulda/controller/routing/macros.rb
@@ -1,47 +0,0 @@
-module ThoughtBot
- module Shoulda
- module Controller
- module Routing
- module Macros
- # Macro that creates a routing test. It tries to use the given HTTP
- # +method+ on the given +path+, and asserts that it routes to the
- # given +options+.
- #
- # If you don't specify a :controller, it will try to guess the controller
- # based on the current test.
- #
- # +to_param+ is called on the +options+ given.
- #
- # Examples:
- #
- # should_route :get, '/posts', :action => :index
- # should_route :post, '/posts', :controller => :posts, :action => :create
- # should_route :get, '/posts/1', :action => :show, :id => 1
- # should_route :put, '/posts/1', :action => :update, :id => "1"
- # should_route :delete, '/posts/1', :action => :destroy, :id => 1
- # should_route :get, '/posts/new', :action => :new
- #
- def should_route(method, path, options)
- unless options[:controller]
- options[:controller] = self.name.gsub(/ControllerTest$/, '').tableize
- end
- options[:controller] = options[:controller].to_s
- options[:action] = options[:action].to_s
-
- populated_path = path.dup
- options.each do |key, value|
- options[key] = value.to_param if value.respond_to? :to_param
- populated_path.gsub!(key.inspect, value.to_s)
- end
-
- should_name = "route #{method.to_s.upcase} #{populated_path} to/from #{options.inspect}"
-
- should should_name do
- assert_routing({:method => method, :path => populated_path}, options)
- end
- end
- end
- end
- end
- end
-end
View
2 tasks/shoulda.rake
@@ -1 +1 @@
-require 'shoulda/tasks'
+load File.join(File.dirname(__FILE__), "..", "lib", "shoulda", "tasks.rb")
View
34 test/fail_macros.rb
@@ -0,0 +1,34 @@
+module Thoughtbot
+ module Shoulda
+ class << self
+ attr_accessor :expected_exceptions
+ end
+
+ # Enables the core shoulda test suite to test for failure scenarios. For
+ # example, to ensure that a set of test macros should fail, do this:
+ #
+ # should_fail do
+ # should_require_attributes :comments
+ # should_protect_attributes :name
+ # end
+ def should_fail(&block)
+ context "should fail when trying to run:" do
+ Shoulda.expected_exceptions = [Test::Unit::AssertionFailedError]
+ yield block
+ Shoulda.expected_exceptions = nil
+ end
+ end
+
+ class Context
+ # alias_method_chain hack to allow the should_fail macro to work
+ def should_with_failure_scenario(name, options = {}, &block)
+ if Shoulda.expected_exceptions
+ expected_exceptions = Shoulda.expected_exceptions
+ failure_block = lambda { assert_raise(*expected_exceptions, &block.bind(self)) }
+ end
+ should_without_failure_scenario(name, options, &(failure_block || block))
+ end
+ alias_method_chain :should, :failure_scenario
+ end
+ end
+end
View
4 test/rails_root/config/environment.rb
@@ -1,6 +1,6 @@
# Specifies gem version of Rails to use when vendor/rails is not present
old_verbose, $VERBOSE = $VERBOSE, nil
-RAILS_GEM_VERSION = '> 2.1.0' unless defined? RAILS_GEM_VERSION
+RAILS_GEM_VERSION = '>= 2.1.0' unless defined? RAILS_GEM_VERSION
$VERBOSE = old_verbose
require File.join(File.dirname(__FILE__), 'boot')
@@ -10,5 +10,5 @@
config.cache_classes = false
config.whiny_nils = true
end
-
+
# Dependencies.log_activity = true
View
10 test/test_helper.rb
@@ -5,15 +5,15 @@
rails_root = File.dirname(__FILE__) + '/rails_root'
require "#{rails_root}/config/environment.rb"
-
+
# Load the testing framework
require 'test_help'
silence_warnings { RAILS_ENV = ENV['RAILS_ENV'] }
-
+
# Run the migrations
ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
-
+
# Setup the fixtures path
Test::Unit::TestCase.fixture_path = File.join(File.dirname(__FILE__), "fixtures")
@@ -25,7 +25,9 @@ def create_fixtures(*table_names)
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
end
end
-
+
self.use_transactional_fixtures = false
self.use_instantiated_fixtures = false
end
+
+require 'test/fail_macros'
View
4 test/unit/tag_test.rb
@@ -7,4 +7,8 @@ class TagTest < Test::Unit::TestCase
should_ensure_length_at_least :name, 2
should_protect_attributes :secret
+
+ should_fail do
+ should_protect_attributes :name
+ end
end
View
4 test/unit/user_test.rb
@@ -44,4 +44,8 @@ class UserTest < Test::Unit::TestCase
should_only_allow_numeric_values_for :ssn
should_have_readonly_attributes :name
+
+ should_fail do
+ should_protect_attributes :name, :age
+ end
end

0 comments on commit f01e8b5

Please sign in to comment.