shoulda 2.11.0 → 2.11.1

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 (43) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +5 -5
  2. data/README.rdoc +72 -89
  3. data/Rakefile +30 -13
  4. data/lib/shoulda.rb +5 -3
  5. data/lib/shoulda/action_controller/matchers.rb +5 -5
  6. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +1 -1
  7. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +9 -9
  8. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +12 -12
  9. data/lib/shoulda/action_controller/matchers/route_matcher.rb +1 -1
  10. data/lib/shoulda/action_mailer/matchers/have_sent_email.rb +17 -26
  11. data/lib/shoulda/active_record/helpers.rb +2 -2
  12. data/lib/shoulda/active_record/macros.rb +2 -2
  13. data/lib/shoulda/active_record/matchers/association_matcher.rb +8 -8
  14. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +1 -1
  15. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +2 -2
  16. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +11 -11
  17. data/lib/shoulda/active_record/matchers/have_db_index_matcher.rb +8 -8
  18. data/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb +2 -4
  19. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +3 -3
  20. data/lib/shoulda/active_record/matchers/validation_matcher.rb +0 -1
  21. data/lib/shoulda/assertions.rb +2 -2
  22. data/lib/shoulda/autoload_macros.rb +20 -20
  23. data/lib/shoulda/context.rb +2 -2
  24. data/lib/shoulda/{rspec.rb → integrations/rspec.rb} +0 -0
  25. data/lib/shoulda/integrations/rspec2.rb +22 -0
  26. data/lib/shoulda/{test_unit.rb → integrations/test_unit.rb} +0 -0
  27. data/lib/shoulda/macros.rb +32 -2
  28. data/lib/shoulda/tasks/yaml_to_shoulda.rake +11 -11
  29. data/lib/shoulda/version.rb +1 -1
  30. data/rails/init.rb +2 -2
  31. data/test/matchers/action_mailer/have_sent_email_test.rb +27 -10
  32. data/test/other/context_test.rb +22 -22
  33. data/test/other/convert_to_should_syntax_test.rb +1 -1
  34. data/test/other/private_helpers_test.rb +1 -1
  35. data/test/other/should_test.rb +12 -12
  36. data/test/rails2_model_builder.rb +4 -4
  37. data/test/rails2_root/log/test.log +5852 -0
  38. data/test/rails3_model_builder.rb +4 -4
  39. data/test/rails3_root/Gemfile +1 -1
  40. data/test/rails3_root/db/test.sqlite3 +0 -0
  41. data/test/rails3_root/log/test.log +5056 -0
  42. data/test/unit/flea_test.rb +1 -1
  43. metadata +5 -4
@@ -71,7 +71,7 @@ module Shoulda # :nodoc:
71
71
 
72
72
  def route_recognized?
73
73
  begin
74
- @context.send(:assert_routing,
74
+ @context.send(:assert_routing,
75
75
  { :method => @method, :path => @path },
76
76
  @params)
77
77
 
@@ -54,35 +54,11 @@ module Shoulda # :nodoc:
54
54
  end
55
55
 
56
56
  def failure_message
57
- msg = "expected a sent email"
58
- msg += " with subject #{@email_subject.inspect}" if @subject_failed
59
- msg += " with body #{@body.inspect}" if @body_failed
60
- msg += " from #{@sender.inspect}" if @sender_failed
61
- msg += " to #{@recipient.inspect}" if @recipient_failed
62
- if anything_failed?
63
- msg += " but got"
64
- msg += " the subject #{@mail.subject.inspect}" if @subject_failed
65
- msg += " the body #{@mail.body.inspect}" if @body_failed
66
- msg += " from #{@mail.from.inspect}" if @sender_failed
67
- msg += " to #{@mail.to.inspect}" if @recipient_failed
68
- end
69
- msg
57
+ "Expected #{expectation}"
70
58
  end
71
59
 
72
60
  def negative_failure_message
73
- msg = "expected no sent email"
74
- msg += " with subject #{@email_subject.inspect}" if @subject_failed
75
- msg += " with body #{@body.inspect}" if @body_failed
76
- msg += " from #{@sender.inspect}" if @sender_failed
77
- msg += " to #{@recipient.inspect}" if @recipient_failed
78
- if anything_failed?
79
- msg += " but got"
80
- msg += " the subject #{@mail.subject.inspect}" if @subject_failed
81
- msg += " the body #{@mail.body.inspect}" if @body_failed
82
- msg += " from #{@mail.from.inspect}" if @sender_failed
83
- msg += " to #{@mail.to.inspect}" if @recipient_failed
84
- end
85
- msg
61
+ "Did not expect #{expectation}"
86
62
  end
87
63
 
88
64
  def description
@@ -91,6 +67,21 @@ module Shoulda # :nodoc:
91
67
 
92
68
  private
93
69
 
70
+ def expectation
71
+ expectation = "sent email"
72
+ expectation << " with subject #{@email_subject.inspect}" if @subject_failed
73
+ expectation << " with body #{@body.inspect}" if @body_failed
74
+ expectation << " from #{@sender.inspect}" if @sender_failed
75
+ expectation << " to #{@recipient.inspect}" if @recipient_failed
76
+ expectation << "\nDeliveries:\n#{inspect_deliveries}"
77
+ end
78
+
79
+ def inspect_deliveries
80
+ ::ActionMailer::Base.deliveries.map do |delivery|
81
+ "#{delivery.subject.inspect} to #{delivery.to.inspect}"
82
+ end.join("\n")
83
+ end
84
+
94
85
  def anything_failed?
95
86
  @subject_failed || @body_failed || @sender_failed || @recipient_failed
96
87
  end
@@ -2,8 +2,8 @@ module Shoulda # :nodoc:
2
2
  module ActiveRecord # :nodoc:
3
3
  module Helpers
4
4
  def pretty_error_messages(obj) # :nodoc:
5
- obj.errors.map do |a, m|
6
- msg = "#{a} #{m}"
5
+ obj.errors.map do |a, m|
6
+ msg = "#{a} #{m}"
7
7
  msg << " (#{obj.send(a).inspect})" unless a.to_sym == :base
8
8
  end
9
9
  end
@@ -377,7 +377,7 @@ module Shoulda # :nodoc:
377
377
  #
378
378
  # Ensure that the given columns are defined on the models backing SQL table.
379
379
  # Also aliased to should_have_db_column for readability.
380
- # Takes the same options available in migrations:
380
+ # Takes the same options available in migrations:
381
381
  # :type, :precision, :limit, :default, :null, and :scale
382
382
  #
383
383
  # Examples:
@@ -390,7 +390,7 @@ module Shoulda # :nodoc:
390
390
  #
391
391
  def should_have_db_columns(*columns)
392
392
  ::ActiveSupport::Deprecation.warn("use: should have_db_column")
393
- column_type, precision, limit, default, null, scale, sql_type =
393
+ column_type, precision, limit, default, null, scale, sql_type =
394
394
  get_options!(columns, :type, :precision, :limit,
395
395
  :default, :null, :scale, :sql_type)
396
396
  columns.each do |name|
@@ -20,9 +20,9 @@ module Shoulda # :nodoc:
20
20
  # dependent option.
21
21
  #
22
22
  # Example:
23
- # it { should_have_many(:friends) }
24
- # it { should_have_many(:enemies).through(:friends) }
25
- # it { should_have_many(:enemies).dependent(:destroy) }
23
+ # it { should have_many(:friends) }
24
+ # it { should have_many(:enemies).through(:friends) }
25
+ # it { should have_many(:enemies).dependent(:destroy) }
26
26
  #
27
27
  def have_many(name)
28
28
  AssociationMatcher.new(:has_many, name)
@@ -70,10 +70,10 @@ module Shoulda # :nodoc:
70
70
 
71
71
  def matches?(subject)
72
72
  @subject = subject
73
- association_exists? &&
74
- macro_correct? &&
75
- foreign_key_exists? &&
76
- through_association_valid? &&
73
+ association_exists? &&
74
+ macro_correct? &&
75
+ foreign_key_exists? &&
76
+ through_association_valid? &&
77
77
  dependent_correct? &&
78
78
  join_table_exists?
79
79
  end
@@ -160,7 +160,7 @@ module Shoulda # :nodoc:
160
160
  end
161
161
 
162
162
  def join_table_exists?
163
- if @macro != :has_and_belongs_to_many ||
163
+ if @macro != :has_and_belongs_to_many ||
164
164
  ::ActiveRecord::Base.connection.tables.include?(join_table.to_s)
165
165
  true
166
166
  else
@@ -57,7 +57,7 @@ module Shoulda # :nodoc:
57
57
  @low_message ||= :inclusion
58
58
  @high_message ||= :inclusion
59
59
 
60
- disallows_lower_value &&
60
+ disallows_lower_value &&
61
61
  allows_minimum_value &&
62
62
  disallows_higher_value &&
63
63
  allows_maximum_value
@@ -84,7 +84,7 @@ module Shoulda # :nodoc:
84
84
  def matches?(subject)
85
85
  super(subject)
86
86
  translate_messages!
87
- disallows_lower_length &&
87
+ disallows_lower_length &&
88
88
  allows_minimum_length &&
89
89
  ((@minimum == @maximum) ||
90
90
  (disallows_higher_length &&
@@ -106,7 +106,7 @@ module Shoulda # :nodoc:
106
106
  end
107
107
 
108
108
  def disallows_lower_length
109
- @minimum == 0 ||
109
+ @minimum == 0 ||
110
110
  @minimum.nil? ||
111
111
  disallows_length_of(@minimum - 1, @short_message)
112
112
  end
@@ -24,12 +24,12 @@ module Shoulda # :nodoc:
24
24
  @macro = macro
25
25
  @column = column
26
26
  end
27
-
27
+
28
28
  def of_type(column_type)
29
29
  @column_type = column_type
30
30
  self
31
31
  end
32
-
32
+
33
33
  def with_options(opts = {})
34
34
  @precision = opts[:precision]
35
35
  @limit = opts[:limit]
@@ -41,8 +41,8 @@ module Shoulda # :nodoc:
41
41
 
42
42
  def matches?(subject)
43
43
  @subject = subject
44
- column_exists? &&
45
- correct_column_type? &&
44
+ column_exists? &&
45
+ correct_column_type? &&
46
46
  correct_precision? &&
47
47
  correct_limit? &&
48
48
  correct_default? &&
@@ -80,7 +80,7 @@ module Shoulda # :nodoc:
80
80
  false
81
81
  end
82
82
  end
83
-
83
+
84
84
  def correct_column_type?
85
85
  return true if @column_type.nil?
86
86
  if matched_column.type.to_s == @column_type.to_s
@@ -91,7 +91,7 @@ module Shoulda # :nodoc:
91
91
  false
92
92
  end
93
93
  end
94
-
94
+
95
95
  def correct_precision?
96
96
  return true if @precision.nil?
97
97
  if matched_column.precision.to_s == @precision.to_s
@@ -103,7 +103,7 @@ module Shoulda # :nodoc:
103
103
  false
104
104
  end
105
105
  end
106
-
106
+
107
107
  def correct_limit?
108
108
  return true if @limit.nil?
109
109
  if matched_column.limit.to_s == @limit.to_s
@@ -115,7 +115,7 @@ module Shoulda # :nodoc:
115
115
  false
116
116
  end
117
117
  end
118
-
118
+
119
119
  def correct_default?
120
120
  return true if @default.nil?
121
121
  if matched_column.default.to_s == @default.to_s
@@ -127,7 +127,7 @@ module Shoulda # :nodoc:
127
127
  false
128
128
  end
129
129
  end
130
-
130
+
131
131
  def correct_null?
132
132
  return true if @null.nil?
133
133
  if matched_column.null.to_s == @null.to_s
@@ -139,7 +139,7 @@ module Shoulda # :nodoc:
139
139
  false
140
140
  end
141
141
  end
142
-
142
+
143
143
  def correct_scale?
144
144
  return true if @scale.nil?
145
145
  if matched_column.scale.to_s == @scale.to_s
@@ -150,7 +150,7 @@ module Shoulda # :nodoc:
150
150
  false
151
151
  end
152
152
  end
153
-
153
+
154
154
  def matched_column
155
155
  model_class.columns.detect { |each| each.name == @column.to_s }
156
156
  end
@@ -27,7 +27,7 @@ module Shoulda # :nodoc:
27
27
  @macro = macro
28
28
  @columns = normalize_columns_to_array(columns)
29
29
  end
30
-
30
+
31
31
  def unique(unique)
32
32
  @unique = unique
33
33
  self
@@ -51,11 +51,11 @@ module Shoulda # :nodoc:
51
51
  end
52
52
 
53
53
  protected
54
-
54
+
55
55
  def index_exists?
56
56
  ! matched_index.nil?
57
57
  end
58
-
58
+
59
59
  def correct_unique?
60
60
  return true if @unique.nil?
61
61
  if matched_index.unique == @unique
@@ -66,7 +66,7 @@ module Shoulda # :nodoc:
66
66
  false
67
67
  end
68
68
  end
69
-
69
+
70
70
  def matched_index
71
71
  indexes.detect { |each| each.columns == @columns }
72
72
  end
@@ -74,11 +74,11 @@ module Shoulda # :nodoc:
74
74
  def model_class
75
75
  @subject.class
76
76
  end
77
-
77
+
78
78
  def table_name
79
79
  model_class.table_name
80
80
  end
81
-
81
+
82
82
  def indexes
83
83
  ::ActiveRecord::Base.connection.indexes(table_name)
84
84
  end
@@ -86,7 +86,7 @@ module Shoulda # :nodoc:
86
86
  def expectation
87
87
  expected = "#{model_class.name} to #{description}"
88
88
  end
89
-
89
+
90
90
  def index_type
91
91
  case @unique
92
92
  when nil
@@ -97,7 +97,7 @@ module Shoulda # :nodoc:
97
97
  'unique'
98
98
  end
99
99
  end
100
-
100
+
101
101
  def normalize_columns_to_array(columns)
102
102
  if columns.class == Array
103
103
  columns.collect { |each| each.to_s }
@@ -25,7 +25,7 @@ module Shoulda # :nodoc:
25
25
  end
26
26
 
27
27
  class ValidateFormatOfMatcher < ValidationMatcher # :nodoc:
28
-
28
+
29
29
  def initialize(attribute)
30
30
  super
31
31
  end
@@ -34,13 +34,12 @@ module Shoulda # :nodoc:
34
34
  @expected_message = message if message
35
35
  self
36
36
  end
37
-
37
+
38
38
  def with(value)
39
39
  raise "You may not call both with and not_with" if @value_to_fail
40
40
  @value_to_pass = value
41
41
  self
42
42
  end
43
-
44
43
 
45
44
  def not_with(value)
46
45
  raise "You may not call both with and not_with" if @value_to_pass
@@ -48,7 +47,6 @@ module Shoulda # :nodoc:
48
47
  self
49
48
  end
50
49
 
51
-
52
50
  def matches?(subject)
53
51
  super(subject)
54
52
  @expected_message ||= :blank
@@ -67,8 +67,8 @@ module Shoulda # :nodoc:
67
67
  def matches?(subject)
68
68
  @subject = subject.class.new
69
69
  @expected_message ||= :taken
70
- find_existing &&
71
- set_scoped_attributes &&
70
+ find_existing &&
71
+ set_scoped_attributes &&
72
72
  validate_attribute &&
73
73
  validate_after_scope_change
74
74
  end
@@ -121,7 +121,7 @@ module Shoulda # :nodoc:
121
121
  @subject.send("#{scope}=", next_value)
122
122
 
123
123
  if allows_value_of(existing_value, @expected_message)
124
- @negative_failure_message <<
124
+ @negative_failure_message <<
125
125
  " (with different value of #{scope})"
126
126
  true
127
127
  else
@@ -18,7 +18,6 @@ module Shoulda # :nodoc:
18
18
  @subject = subject
19
19
  false
20
20
  end
21
-
22
21
 
23
22
  private
24
23
 
@@ -26,7 +26,7 @@ module Shoulda # :nodoc:
26
26
  case x
27
27
  when Regexp
28
28
  assert(collection.detect { |e| e =~ x }, msg)
29
- else
29
+ else
30
30
  assert(collection.include?(x), msg)
31
31
  end
32
32
  end
@@ -39,7 +39,7 @@ module Shoulda # :nodoc:
39
39
  case x
40
40
  when Regexp
41
41
  assert(!collection.detect { |e| e =~ x }, msg)
42
- else
42
+ else
43
43
  assert(!collection.include?(x), msg)
44
44
  end
45
45
  end
@@ -1,40 +1,40 @@
1
1
  module Shoulda # :nodoc:
2
- # Call autoload_macros when you want to load test macros automatically in a non-Rails
2
+ # Call autoload_macros when you want to load test macros automatically in a non-Rails
3
3
  # project (it's done automatically for Rails projects).
4
4
  # You don't need to specify ROOT/test/shoulda_macros explicitly. Your custom macros
5
5
  # are loaded automatically when you call autoload_macros.
6
6
  #
7
7
  # The first argument is the path to you application's root directory.
8
- # All following arguments are directories relative to your root, which contain
9
- # shoulda_macros subdirectories. These directories support the same kinds of globs as the
8
+ # All following arguments are directories relative to your root, which contain
9
+ # shoulda_macros subdirectories. These directories support the same kinds of globs as the
10
10
  # Dir class.
11
- #
11
+ #
12
12
  # Basic usage (from a test_helper):
13
13
  # Shoulda.autoload_macros(File.dirname(__FILE__) + '/..')
14
- # will load everything in
15
- # - your_app/test/shoulda_macros
14
+ # will load everything in
15
+ # - your_app/test/shoulda_macros
16
16
  #
17
- # To load vendored macros as well:
17
+ # To load vendored macros as well:
18
18
  # Shoulda.autoload_macros(APP_ROOT, 'vendor/*')
19
- # will load everything in
20
- # - APP_ROOT/vendor/*/shoulda_macros
21
- # - APP_ROOT/test/shoulda_macros
19
+ # will load everything in
20
+ # - APP_ROOT/vendor/*/shoulda_macros
21
+ # - APP_ROOT/test/shoulda_macros
22
22
  #
23
- # To load macros in an app with a vendor directory laid out like Rails':
23
+ # To load macros in an app with a vendor directory laid out like Rails':
24
24
  # Shoulda.autoload_macros(APP_ROOT, 'vendor/{plugins,gems}/*')
25
25
  # or
26
26
  # Shoulda.autoload_macros(APP_ROOT, 'vendor/plugins/*', 'vendor/gems/*')
27
- # will load everything in
28
- # - APP_ROOT/vendor/plugins/*/shoulda_macros
29
- # - APP_ROOT/vendor/gems/*/shoulda_macros
30
- # - APP_ROOT/test/shoulda_macros
27
+ # will load everything in
28
+ # - APP_ROOT/vendor/plugins/*/shoulda_macros
29
+ # - APP_ROOT/vendor/gems/*/shoulda_macros
30
+ # - APP_ROOT/test/shoulda_macros
31
31
  #
32
- # If you prefer to stick testing dependencies away from your production dependencies:
32
+ # If you prefer to stick testing dependencies away from your production dependencies:
33
33
  # Shoulda.autoload_macros(APP_ROOT, 'vendor/*', 'test/vendor/*')
34
- # will load everything in
35
- # - APP_ROOT/vendor/*/shoulda_macros
36
- # - APP_ROOT/test/vendor/*/shoulda_macros
37
- # - APP_ROOT/test/shoulda_macros
34
+ # will load everything in
35
+ # - APP_ROOT/vendor/*/shoulda_macros
36
+ # - APP_ROOT/test/vendor/*/shoulda_macros
37
+ # - APP_ROOT/test/shoulda_macros
38
38
  def self.autoload_macros(root, *dirs)
39
39
  dirs << File.join('test')
40
40
  complete_dirs = dirs.map{|d| File.join(root, d, 'shoulda_macros')}