ratnikov-shoulda 2.0.6.3 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. data/README.rdoc +3 -2
  2. data/Rakefile +1 -1
  3. data/lib/shoulda/active_record/assertions.rb +10 -31
  4. data/lib/shoulda/active_record/helpers.rb +40 -0
  5. data/lib/shoulda/active_record/macros.rb +171 -325
  6. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  7. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
  8. data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
  9. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  10. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  11. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  12. data/lib/shoulda/active_record/matchers/have_index_matcher.rb +105 -0
  13. data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +125 -0
  14. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
  15. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
  16. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
  17. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  18. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  19. data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
  20. data/lib/shoulda/active_record/matchers.rb +42 -0
  21. data/lib/shoulda/active_record.rb +4 -0
  22. data/lib/shoulda/assertions.rb +12 -0
  23. data/lib/shoulda/autoload_macros.rb +46 -0
  24. data/lib/shoulda/rails.rb +1 -8
  25. data/lib/shoulda/rspec.rb +5 -0
  26. data/lib/shoulda/tasks/list_tests.rake +6 -1
  27. data/lib/shoulda/test_unit.rb +19 -0
  28. data/lib/shoulda.rb +5 -17
  29. data/rails/init.rb +1 -1
  30. data/test/README +2 -2
  31. data/test/fail_macros.rb +2 -2
  32. data/test/matchers/allow_mass_assignment_of_matcher_test.rb +68 -0
  33. data/test/matchers/allow_value_matcher_test.rb +41 -0
  34. data/test/matchers/association_matcher_test.rb +258 -0
  35. data/test/matchers/ensure_inclusion_of_matcher_test.rb +80 -0
  36. data/test/matchers/ensure_length_of_matcher_test.rb +158 -0
  37. data/test/matchers/have_db_column_matcher_test.rb +169 -0
  38. data/test/matchers/have_index_matcher_test.rb +74 -0
  39. data/test/matchers/have_named_scope_matcher_test.rb +65 -0
  40. data/test/matchers/have_readonly_attributes_matcher_test.rb +29 -0
  41. data/test/matchers/validate_acceptance_of_matcher_test.rb +44 -0
  42. data/test/matchers/validate_numericality_of_matcher_test.rb +52 -0
  43. data/test/matchers/validate_presence_of_matcher_test.rb +86 -0
  44. data/test/matchers/validate_uniqueness_of_matcher_test.rb +141 -0
  45. data/test/model_builder.rb +61 -0
  46. data/test/other/autoload_macro_test.rb +18 -0
  47. data/test/other/helpers_test.rb +58 -0
  48. data/test/rails_root/config/database.yml +1 -1
  49. data/test/rails_root/config/environments/{sqlite3.rb → test.rb} +0 -0
  50. data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
  51. data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  52. data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  53. data/test/test_helper.rb +3 -1
  54. data/test/unit/address_test.rb +1 -1
  55. data/test/unit/dog_test.rb +1 -1
  56. data/test/unit/post_test.rb +4 -4
  57. data/test/unit/product_test.rb +2 -2
  58. data/test/unit/tag_test.rb +2 -1
  59. data/test/unit/user_test.rb +8 -7
  60. metadata +49 -3
data/README.rdoc CHANGED
@@ -7,6 +7,7 @@ Helpers:: #context and #should give you rSpec like test blocks.
7
7
  Macros:: Generate hundreds of lines of Controller and ActiveRecord tests with these powerful macros.
8
8
  They get you started quickly, and can help you ensure that your application is conforming to best practices.
9
9
  Assertions:: Many common rails testing idioms have been distilled into a set of useful assertions.
10
+ Matchers:: Rspec-compatible matchers providing the same tests as Shoulda macros.
10
11
 
11
12
  = Usage
12
13
 
@@ -54,8 +55,8 @@ Quick macro tests for your ActiveRecord associations and validations:
54
55
  should_have_many :tags, :through => :taggings
55
56
 
56
57
  should_require_unique_attributes :title
57
- should_require_attributes :body, :message => /wtf/
58
- should_require_attributes :title
58
+ should_validate_presence_of :body, :message => /wtf/
59
+ should_validate_presence_of :title
59
60
  should_only_allow_numeric_values_for :user_id
60
61
  end
61
62
 
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ require 'shoulda'
8
8
  load 'tasks/shoulda.rake'
9
9
 
10
10
  # Test::Unit::UI::VERBOSE
11
- test_files_pattern = 'test/{unit,functional,other}/**/*_test.rb'
11
+ test_files_pattern = 'test/{unit,functional,other,matchers}/**/*_test.rb'
12
12
  Rake::TestTask.new do |t|
13
13
  t.libs << 'lib'
14
14
  t.pattern = test_files_pattern
@@ -32,11 +32,12 @@ module Shoulda # :nodoc:
32
32
  #
33
33
  # @product = Product.new(:tangible => false)
34
34
  # assert_good_value(Product, :price, "0")
35
- def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = //)
35
+ def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = nil)
36
36
  object = get_instance_of(object_or_klass)
37
- object.send("#{attribute}=", value)
38
- object.valid?
39
- assert_does_not_contain(object.errors.on(attribute), error_message_to_avoid, "when set to #{value.inspect}")
37
+ matcher = allow_value(value).
38
+ for(attribute).
39
+ with_message(error_message_to_avoid)
40
+ assert_accepts(matcher, object)
40
41
  end
41
42
 
42
43
  # Asserts that an Active Record model invalidates the passed
@@ -56,34 +57,12 @@ module Shoulda # :nodoc:
56
57
  # @product = Product.new(:tangible => true)
57
58
  # assert_bad_value(Product, :price, "0")
58
59
  def assert_bad_value(object_or_klass, attribute, value,
59
- error_message_to_expect = self.class.default_error_message(:invalid))
60
+ error_message_to_expect = nil)
60
61
  object = get_instance_of(object_or_klass)
61
- object.send("#{attribute}=", value)
62
- assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}"
63
- assert object.errors.on(attribute), "There are no errors on #{attribute} after being set to #{value.inspect}"
64
- assert_contains(object.errors.on(attribute), error_message_to_expect, "when set to #{value.inspect}")
65
- end
66
-
67
- def pretty_error_messages(obj)
68
- obj.errors.map do |a, m|
69
- msg = "#{a} #{m}"
70
- msg << " (#{obj.send(a).inspect})" unless a.to_sym == :base
71
- end
72
- end
73
-
74
- private
75
-
76
- def get_instance_of(object_or_klass)
77
- if object_or_klass.is_a?(Class)
78
- klass = object_or_klass
79
- instance_variable_get("@#{instance_variable_name_for(klass)}") || klass.new
80
- else
81
- object_or_klass
82
- end
83
- end
84
-
85
- def instance_variable_name_for(klass)
86
- klass.to_s.split('::').last.underscore
62
+ matcher = allow_value(value).
63
+ for(attribute).
64
+ with_message(error_message_to_expect)
65
+ assert_rejects(matcher, object)
87
66
  end
88
67
  end
89
68
  end
@@ -0,0 +1,40 @@
1
+ module Shoulda # :nodoc:
2
+ module ActiveRecord # :nodoc:
3
+ module Helpers
4
+ def pretty_error_messages(obj) # :nodoc:
5
+ obj.errors.map do |a, m|
6
+ msg = "#{a} #{m}"
7
+ msg << " (#{obj.send(a).inspect})" unless a.to_sym == :base
8
+ end
9
+ end
10
+
11
+ def get_instance_of(object_or_klass)
12
+ if object_or_klass.is_a?(Class)
13
+ klass = object_or_klass
14
+ instance_variable_get("@#{instance_variable_name_for(klass)}") || klass.new
15
+ else
16
+ object_or_klass
17
+ end
18
+ end
19
+
20
+ def instance_variable_name_for(klass)
21
+ klass.to_s.split('::').last.underscore
22
+ end
23
+
24
+ # Helper method that determines the default error message used by Active
25
+ # Record. Works for both existing Rails 2.1 and Rails 2.2 with the newly
26
+ # introduced I18n module used for localization.
27
+ #
28
+ # default_error_message(:blank)
29
+ # default_error_message(:too_short, :count => 5)
30
+ # default_error_message(:too_long, :count => 60)
31
+ def default_error_message(key, values = {})
32
+ if Object.const_defined?(:I18n) # Rails >= 2.2
33
+ I18n.translate("activerecord.errors.messages.#{key}", values)
34
+ else # Rails <= 2.1.x
35
+ ::ActiveRecord::Errors.default_error_messages[key] % values[:count]
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end