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.
- data/README.rdoc +3 -2
- data/Rakefile +1 -1
- data/lib/shoulda/active_record/assertions.rb +10 -31
- data/lib/shoulda/active_record/helpers.rb +40 -0
- data/lib/shoulda/active_record/macros.rb +171 -325
- data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
- data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
- data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
- data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
- data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
- data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
- data/lib/shoulda/active_record/matchers/have_index_matcher.rb +105 -0
- data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +125 -0
- data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
- data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
- data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
- data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
- data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
- data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
- data/lib/shoulda/active_record/matchers.rb +42 -0
- data/lib/shoulda/active_record.rb +4 -0
- data/lib/shoulda/assertions.rb +12 -0
- data/lib/shoulda/autoload_macros.rb +46 -0
- data/lib/shoulda/rails.rb +1 -8
- data/lib/shoulda/rspec.rb +5 -0
- data/lib/shoulda/tasks/list_tests.rake +6 -1
- data/lib/shoulda/test_unit.rb +19 -0
- data/lib/shoulda.rb +5 -17
- data/rails/init.rb +1 -1
- data/test/README +2 -2
- data/test/fail_macros.rb +2 -2
- data/test/matchers/allow_mass_assignment_of_matcher_test.rb +68 -0
- data/test/matchers/allow_value_matcher_test.rb +41 -0
- data/test/matchers/association_matcher_test.rb +258 -0
- data/test/matchers/ensure_inclusion_of_matcher_test.rb +80 -0
- data/test/matchers/ensure_length_of_matcher_test.rb +158 -0
- data/test/matchers/have_db_column_matcher_test.rb +169 -0
- data/test/matchers/have_index_matcher_test.rb +74 -0
- data/test/matchers/have_named_scope_matcher_test.rb +65 -0
- data/test/matchers/have_readonly_attributes_matcher_test.rb +29 -0
- data/test/matchers/validate_acceptance_of_matcher_test.rb +44 -0
- data/test/matchers/validate_numericality_of_matcher_test.rb +52 -0
- data/test/matchers/validate_presence_of_matcher_test.rb +86 -0
- data/test/matchers/validate_uniqueness_of_matcher_test.rb +141 -0
- data/test/model_builder.rb +61 -0
- data/test/other/autoload_macro_test.rb +18 -0
- data/test/other/helpers_test.rb +58 -0
- data/test/rails_root/config/database.yml +1 -1
- data/test/rails_root/config/environments/{sqlite3.rb → test.rb} +0 -0
- data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
- data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
- data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
- data/test/test_helper.rb +3 -1
- data/test/unit/address_test.rb +1 -1
- data/test/unit/dog_test.rb +1 -1
- data/test/unit/post_test.rb +4 -4
- data/test/unit/product_test.rb +2 -2
- data/test/unit/tag_test.rb +2 -1
- data/test/unit/user_test.rb +8 -7
- 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
|
-
|
|
58
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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 =
|
|
60
|
+
error_message_to_expect = nil)
|
|
60
61
|
object = get_instance_of(object_or_klass)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|