shoulda 2.11.0 → 2.11.1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -116,7 +116,7 @@ module Shoulda
116
116
  # context "on GET" do
117
117
  # setup { get :index }
118
118
  #
119
- # should_respond_with :success
119
+ # should respond_with(:success)
120
120
  #
121
121
  # # runs before "get :index"
122
122
  # before_should "find all users" do
@@ -215,7 +215,7 @@ module Shoulda
215
215
  # subject { User.first }
216
216
  #
217
217
  # # uses the existing user
218
- # should_validate_uniqueness_of :email
218
+ # should validate_uniqueness_of(:email)
219
219
  # end
220
220
  def subject(&block)
221
221
  @subject_block = block
File without changes
@@ -0,0 +1,22 @@
1
+ require 'shoulda/active_record/matchers'
2
+ require 'shoulda/action_controller/matchers'
3
+ require 'shoulda/action_mailer/matchers'
4
+
5
+ # :enddoc:
6
+
7
+ module RSpec
8
+ module Matchers
9
+ include Shoulda::ActiveRecord::Matchers
10
+ end
11
+
12
+ module Rails
13
+ module ControllerExampleGroup
14
+ include Shoulda::ActionController::Matchers
15
+ end
16
+
17
+ module MailerExampleGroup
18
+ include Shoulda::ActionMailer::Matchers
19
+ end
20
+ end
21
+ end
22
+
@@ -36,8 +36,23 @@ module Shoulda # :nodoc:
36
36
  #
37
37
  # # Assert the value changed to "new:"
38
38
  # should_change("the post title", :to => "new") { @post.title }
39
+ #
40
+ # This macro was deprecated because these tests aren't as valuable as
41
+ # alternative tests that explicitly test the final state.
42
+ #
43
+ # Consider an alternative:
44
+ #
45
+ # context "updating a post" do
46
+ # setup do
47
+ # @post = Post.create(:title => "old")
48
+ # put :update, :post => {:title => "new"}, :id => @post.to_param
49
+ # end
50
+ # should "update the title" do
51
+ # assert_equal "new", @post.reload.title
52
+ # end
53
+ # end
39
54
  def should_change(description, options = {}, &block)
40
- ::ActiveSupport::Deprecation.warn
55
+ ::ActiveSupport::Deprecation.warn("Not considered a useful test. Instead, test the end state explicitly.")
41
56
  by, from, to = get_options!([options], :by, :from, :to)
42
57
  stmt = "change #{description}"
43
58
  stmt << " from #{from.inspect}" if from
@@ -69,8 +84,23 @@ module Shoulda # :nodoc:
69
84
  # setup { @post.update_attributes(:title => "new") }
70
85
  # should_not_change("the number of posts") { Post.count }
71
86
  # end
87
+ #
88
+ # This macro was deprecated because these tests aren't as valuable as
89
+ # alternative tests that explicitly test the final state.
90
+ #
91
+ # Consider an alternative:
92
+ #
93
+ # context "updating a post" do
94
+ # setup do
95
+ # @post = Post.create(:title => "old")
96
+ # put :update, :post => {:title => ""}, :id => @post.to_param
97
+ # end
98
+ # should "not update the title" do
99
+ # assert_equal "old", @post.reload.title
100
+ # end
101
+ # end
72
102
  def should_not_change(description, &block)
73
- ::ActiveSupport::Deprecation.warn
103
+ ::ActiveSupport::Deprecation.warn("Not considered a useful test. Instead, test the end state explicitly.")
74
104
  before = lambda { @_before_should_not_change = block.bind(self).call }
75
105
  should "not change #{description}", :before => before do
76
106
  new_value = block.bind(self).call
@@ -1,28 +1,28 @@
1
1
  namespace :shoulda do
2
2
  # From http://blog.internautdesign.com/2007/11/2/a-yaml_to_shoulda-rake-task
3
3
  # David.Lowenfels@gmail.com
4
- desc "Converts a YAML file (FILE=./path/to/yaml) into a Shoulda skeleton"
4
+ desc "Converts a YAML file (FILE=./path/to/yaml) into a Shoulda skeleton"
5
5
  task :from_yaml do
6
6
  require 'yaml'
7
-
7
+
8
8
  def yaml_to_context(hash, indent = 0)
9
9
  indent1 = ' ' * indent
10
10
  indent2 = ' ' * (indent + 1)
11
11
  hash.each_pair do |context, shoulds|
12
- puts indent1 + "context \"#{context}\" do"
13
- puts
12
+ puts indent1 + "context \"#{context}\" do"
13
+ puts
14
14
  shoulds.each do |should|
15
15
  yaml_to_context( should, indent + 1 ) and next if should.is_a?( Hash )
16
- puts indent2 + "should_eventually \"" + should.gsub(/^should +/,'') + "\" do"
17
- puts indent2 + "end"
16
+ puts indent2 + "should_eventually \"" + should.gsub(/^should +/,'') + "\" do"
17
+ puts indent2 + "end"
18
18
  puts
19
19
  end
20
- puts indent1 + "end"
20
+ puts indent1 + "end"
21
21
  end
22
- end
23
-
22
+ end
23
+
24
24
  puts("Please pass in a FILE argument.") and exit unless ENV['FILE']
25
-
25
+
26
26
  yaml_to_context( YAML.load_file( ENV['FILE'] ) )
27
27
  end
28
- end
28
+ end
@@ -1,4 +1,4 @@
1
1
  module Shoulda
2
- VERSION = "2.11.0"
2
+ VERSION = "2.11.1"
3
3
  end
4
4
 
data/rails/init.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  if RAILS_ENV == 'test'
2
2
  if defined? Spec
3
- require 'shoulda/rspec'
3
+ require 'shoulda/integrations/rspec'
4
4
  else
5
- require 'shoulda/rails'
5
+ require 'shoulda/integrations/test_unit'
6
6
  Shoulda.autoload_macros RAILS_ROOT, File.join("vendor", "{plugins,gems}", "*")
7
7
  end
8
8
  end
@@ -1,6 +1,14 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class HaveSentEmailTest < ActiveSupport::TestCase # :nodoc:
4
+ def add_mail_to_deliveries
5
+ if defined?(AbstractController::Rendering)
6
+ ::ActionMailer::Base.deliveries << Mailer.the_email
7
+ else
8
+ ::ActionMailer::Base.deliveries << Mailer.create_the_email
9
+ end
10
+ end
11
+
4
12
  context "an email" do
5
13
  setup do
6
14
  define_mailer :mailer, [:the_email] do
@@ -18,31 +26,40 @@ class HaveSentEmailTest < ActiveSupport::TestCase # :nodoc:
18
26
  end
19
27
  end
20
28
  end
21
- if defined?(AbstractController::Rendering)
22
- ::ActionMailer::Base.deliveries << Mailer.the_email
23
- else
24
- ::ActionMailer::Base.deliveries << Mailer.create_the_email
25
- end
29
+ add_mail_to_deliveries
26
30
  end
27
31
 
32
+ teardown { ::ActionMailer::Base.deliveries.clear }
33
+
28
34
  should "accept based on the subject" do
29
35
  assert_accepts have_sent_email.with_subject(/is spam$/), nil
30
- assert_rejects have_sent_email.with_subject(/totally safe/), nil
36
+ assert_rejects have_sent_email.with_subject(/totally safe/), nil,
37
+ :message => /Expected sent email with subject/
31
38
  end
32
39
 
33
40
  should "accept based on the sender" do
34
41
  assert_accepts have_sent_email.from('do-not-reply@example.com'), nil
35
- assert_rejects have_sent_email.from('you@example.com'), nil
42
+ assert_rejects have_sent_email.from('you@example.com'), nil,
43
+ :message => /Expected sent email from/
36
44
  end
37
45
 
38
46
  should "accept based on the body" do
39
47
  assert_accepts have_sent_email.with_body(/is spam\./), nil
40
- assert_rejects have_sent_email.with_body(/totally safe/), nil
48
+ assert_rejects have_sent_email.with_body(/totally safe/), nil,
49
+ :message => /Expected sent email with body/
41
50
  end
42
51
 
43
- should "accept baed on the recipienct" do
52
+ should "accept based on the recipient" do
44
53
  assert_accepts have_sent_email.to('myself@me.com'), nil
45
- assert_rejects have_sent_email.to('you@example.com'), nil
54
+ assert_rejects have_sent_email.to('you@example.com'), nil,
55
+ :message => /Expected sent email to/
56
+ end
57
+
58
+ should "list all deliveries within failure message" do
59
+ add_mail_to_deliveries
60
+
61
+ assert_rejects have_sent_email.to('you@example.com'), nil,
62
+ :message => /Deliveries:\n"This is spam" to \["myself@me\.com"\]\n"This is spam" to \["myself@me\.com"\]/
46
63
  end
47
64
 
48
65
  should "chain" do
@@ -1,12 +1,12 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class ContextTest < ActiveSupport::TestCase # :nodoc:
4
-
4
+
5
5
  def self.context_macro(&blk)
6
6
  context "with a subcontext made by a macro" do
7
7
  setup { @context_macro = :foo }
8
8
 
9
- merge_block &blk
9
+ merge_block &blk
10
10
  end
11
11
  end
12
12
 
@@ -21,11 +21,11 @@ class ContextTest < ActiveSupport::TestCase # :nodoc:
21
21
  setup do
22
22
  @blah = "blah"
23
23
  end
24
-
24
+
25
25
  should "run the setup block" do
26
26
  assert_equal "blah", @blah
27
27
  end
28
-
28
+
29
29
  should "have name set right" do
30
30
  assert_match(/^test: context with setup block/, self.to_s)
31
31
  end
@@ -34,11 +34,11 @@ class ContextTest < ActiveSupport::TestCase # :nodoc:
34
34
  setup do
35
35
  @blah = "#{@blah} twice"
36
36
  end
37
-
37
+
38
38
  should "be named correctly" do
39
39
  assert_match(/^test: context with setup block and a subcontext should be named correctly/, self.to_s)
40
40
  end
41
-
41
+
42
42
  should "run the setup blocks in order" do
43
43
  assert_equal @blah, "blah twice"
44
44
  end
@@ -64,7 +64,7 @@ class ContextTest < ActiveSupport::TestCase # :nodoc:
64
64
  setup do
65
65
  @blah = "foo"
66
66
  end
67
-
67
+
68
68
  should "have @blah == 'foo'" do
69
69
  assert_equal "foo", @blah
70
70
  end
@@ -73,12 +73,12 @@ class ContextTest < ActiveSupport::TestCase # :nodoc:
73
73
  assert_match(/^test: another context with setup block/, self.to_s)
74
74
  end
75
75
  end
76
-
76
+
77
77
  context "context with method definition" do
78
78
  setup do
79
79
  def hello; "hi"; end
80
80
  end
81
-
81
+
82
82
  should "be able to read that method" do
83
83
  assert_equal "hi", hello
84
84
  end
@@ -87,52 +87,52 @@ class ContextTest < ActiveSupport::TestCase # :nodoc:
87
87
  assert_match(/^test: context with method definition/, self.to_s)
88
88
  end
89
89
  end
90
-
90
+
91
91
  context "another context" do
92
92
  should "not define @blah" do
93
93
  assert_nil @blah
94
94
  end
95
95
  end
96
-
96
+
97
97
  context "context with multiple setups and/or teardowns" do
98
-
98
+
99
99
  cleanup_count = 0
100
-
100
+
101
101
  2.times do |i|
102
102
  setup { cleanup_count += 1 }
103
103
  teardown { cleanup_count -= 1 }
104
104
  end
105
-
105
+
106
106
  2.times do |i|
107
107
  should "call all setups and all teardowns (check ##{i + 1})" do
108
108
  assert_equal 2, cleanup_count
109
109
  end
110
110
  end
111
-
111
+
112
112
  context "subcontexts" do
113
-
113
+
114
114
  2.times do |i|
115
115
  setup { cleanup_count += 1 }
116
116
  teardown { cleanup_count -= 1 }
117
117
  end
118
-
118
+
119
119
  2.times do |i|
120
120
  should "also call all setups and all teardowns in parent and subcontext (check ##{i + 1})" do
121
121
  assert_equal 4, cleanup_count
122
122
  end
123
123
  end
124
-
124
+
125
125
  end
126
-
126
+
127
127
  end
128
-
128
+
129
129
  should_eventually "pass, since it's unimplemented" do
130
130
  flunk "what?"
131
131
  end
132
132
 
133
133
  should_eventually "not require a block when using should_eventually"
134
134
  should "pass without a block, as that causes it to piggyback to should_eventually"
135
-
135
+
136
136
  context "context for testing should piggybacking" do
137
137
  should "call should_eventually as we are not passing a block"
138
138
  end
@@ -164,7 +164,7 @@ class ContextTest < ActiveSupport::TestCase # :nodoc:
164
164
  should "return the result of the block as the subject" do
165
165
  assert_equal @expected, subject
166
166
  end
167
-
167
+
168
168
  context "nested context block without a subject block" do
169
169
  should "return the result of the parent context's subject block" do
170
170
  assert_equal @expected, subject
@@ -41,7 +41,7 @@ class ConvertToShouldSyntaxTest < ActiveSupport::TestCase # :nodoc:
41
41
 
42
42
  end
43
43
  EOS
44
-
44
+
45
45
  FIXTURE_PATH = "./convert_to_should_syntax_fixture.dat"
46
46
 
47
47
  RUBY = ENV['RUBY'] || 'ruby'
@@ -22,7 +22,7 @@ class PrivateHelpersTest < ActiveSupport::TestCase # :nodoc:
22
22
  get_options!(args, :one)
23
23
  end
24
24
  end
25
-
25
+
26
26
  should "return single wanted option" do
27
27
  args = [:a, :b, {:class => Object}]
28
28
  klass = get_options!(args,:class)
@@ -4,11 +4,11 @@ class ShouldTest < ActiveSupport::TestCase # :nodoc:
4
4
  should "be able to define a should statement outside of a context" do
5
5
  assert true
6
6
  end
7
-
7
+
8
8
  should "see the name of my class as ShouldTest" do
9
9
  assert_equal "ShouldTest", self.class.name
10
10
  end
11
-
11
+
12
12
  def self.should_see_class_methods
13
13
  should "be able to see class methods" do
14
14
  assert true
@@ -27,13 +27,13 @@ class ShouldTest < ActiveSupport::TestCase # :nodoc:
27
27
 
28
28
  def self.should_see_blah
29
29
  should "see @blah through a macro" do
30
- assert @blah
30
+ assert @blah
31
31
  end
32
32
  end
33
33
 
34
34
  def self.should_not_see_blah
35
35
  should "not see @blah through a macro" do
36
- assert_nil @blah
36
+ assert_nil @blah
37
37
  end
38
38
  end
39
39
 
@@ -75,12 +75,12 @@ class ShouldTest < ActiveSupport::TestCase # :nodoc:
75
75
  setup do
76
76
  @blah = "blah"
77
77
  end
78
-
78
+
79
79
  should "have @blah == 'blah'" do
80
80
  assert_equal "blah", @blah
81
81
  end
82
82
  should_see_blah
83
-
83
+
84
84
  should "have name set right" do
85
85
  assert_match(/^test: Context with setup block/, self.to_s)
86
86
  end
@@ -89,11 +89,11 @@ class ShouldTest < ActiveSupport::TestCase # :nodoc:
89
89
  setup do
90
90
  @blah = "#{@blah} twice"
91
91
  end
92
-
92
+
93
93
  should "be named correctly" do
94
94
  assert_match(/^test: Context with setup block and a subcontext should be named correctly/, self.to_s)
95
95
  end
96
-
96
+
97
97
  should "run the setup methods in order" do
98
98
  assert_equal @blah, "blah twice"
99
99
  end
@@ -105,7 +105,7 @@ class ShouldTest < ActiveSupport::TestCase # :nodoc:
105
105
  setup do
106
106
  @blah = "foo"
107
107
  end
108
-
108
+
109
109
  should "have @blah == 'foo'" do
110
110
  assert_equal "foo", @blah
111
111
  end
@@ -115,7 +115,7 @@ class ShouldTest < ActiveSupport::TestCase # :nodoc:
115
115
  end
116
116
  should_see_blah
117
117
  end
118
-
118
+
119
119
  should_eventually "pass, since it's a should_eventually" do
120
120
  flunk "what?"
121
121
  end
@@ -152,7 +152,7 @@ class ShouldTest < ActiveSupport::TestCase # :nodoc:
152
152
  should "be good" do; end
153
153
  should "another" do; end
154
154
  end
155
-
155
+
156
156
  names = context.shoulds.map {|s| s[:name]}
157
157
  assert_equal ["another", "be good"], names.sort
158
158
  end
@@ -164,7 +164,7 @@ class ShouldTest < ActiveSupport::TestCase # :nodoc:
164
164
  setup do; "setup"; end
165
165
  teardown do; "teardown"; end
166
166
  end
167
-
167
+
168
168
  assert_equal "setup", context.setup_blocks.first.call
169
169
  assert_equal "teardown", context.teardown_blocks.first.call
170
170
  end