shoulda-matchers 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/.gitignore +11 -0
  2. data/.travis.yml +13 -0
  3. data/Appraisals +4 -6
  4. data/CONTRIBUTING.md +38 -0
  5. data/Gemfile +8 -5
  6. data/Gemfile.lock +77 -41
  7. data/NEWS.md +32 -0
  8. data/README.md +84 -0
  9. data/Rakefile +5 -36
  10. data/features/rails_integration.feature +88 -0
  11. data/features/step_definitions/rails_steps.rb +111 -0
  12. data/features/support/env.rb +5 -0
  13. data/gemfiles/3.0.gemfile +14 -0
  14. data/gemfiles/3.0.gemfile.lock +142 -0
  15. data/gemfiles/3.1.gemfile +16 -0
  16. data/gemfiles/3.1.gemfile.lock +164 -0
  17. data/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +6 -9
  18. data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +1 -3
  19. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +10 -6
  20. data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +1 -4
  21. data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +6 -6
  22. data/lib/shoulda/matchers/action_controller/respond_with_content_type_matcher.rb +11 -10
  23. data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +0 -2
  24. data/lib/shoulda/matchers/action_controller/route_matcher.rb +17 -14
  25. data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +24 -16
  26. data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +52 -15
  27. data/lib/shoulda/matchers/action_mailer.rb +1 -1
  28. data/lib/shoulda/matchers/action_mailer/{have_sent_email.rb → have_sent_email_matcher.rb} +37 -21
  29. data/lib/shoulda/matchers/active_model.rb +1 -0
  30. data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +9 -10
  31. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +60 -33
  32. data/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +0 -1
  33. data/lib/shoulda/matchers/active_model/helpers.rb +13 -9
  34. data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +63 -0
  35. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +38 -8
  36. data/lib/shoulda/matchers/active_model/validation_matcher.rb +1 -5
  37. data/lib/shoulda/matchers/active_record.rb +3 -1
  38. data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +127 -0
  39. data/lib/shoulda/matchers/active_record/association_matcher.rb +19 -7
  40. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +20 -5
  41. data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +4 -10
  42. data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +3 -7
  43. data/lib/shoulda/matchers/active_record/query_the_database_matcher.rb +107 -0
  44. data/lib/shoulda/matchers/active_record/serialize_matcher.rb +111 -0
  45. data/lib/shoulda/matchers/integrations/rspec.rb +0 -1
  46. data/lib/shoulda/matchers/version.rb +1 -1
  47. data/shoulda-matchers.gemspec +30 -0
  48. data/spec/fixtures/addresses.yml +3 -0
  49. data/spec/fixtures/friendships.yml +0 -0
  50. data/spec/fixtures/posts.yml +5 -0
  51. data/spec/fixtures/products.yml +0 -0
  52. data/spec/fixtures/taggings.yml +0 -0
  53. data/spec/fixtures/tags.yml +9 -0
  54. data/spec/fixtures/users.yml +6 -0
  55. data/spec/shoulda/action_controller/assign_to_matcher_spec.rb +61 -0
  56. data/spec/shoulda/action_controller/filter_param_matcher_spec.rb +20 -0
  57. data/spec/shoulda/action_controller/redirect_to_matcher_spec.rb +40 -0
  58. data/spec/shoulda/action_controller/render_template_matcher_spec.rb +69 -0
  59. data/spec/shoulda/action_controller/render_with_layout_matcher_spec.rb +47 -0
  60. data/spec/shoulda/action_controller/respond_with_content_type_matcher_spec.rb +28 -0
  61. data/spec/shoulda/action_controller/respond_with_matcher_spec.rb +83 -0
  62. data/spec/shoulda/action_controller/route_matcher_spec.rb +65 -0
  63. data/spec/shoulda/action_controller/set_session_matcher_spec.rb +46 -0
  64. data/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb +124 -0
  65. data/spec/shoulda/action_mailer/have_sent_email_spec.rb +293 -0
  66. data/spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb +95 -0
  67. data/spec/shoulda/active_model/allow_value_matcher_spec.rb +91 -0
  68. data/spec/shoulda/active_model/ensure_exclusion_of_matcher_spec.rb +57 -0
  69. data/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb +71 -0
  70. data/spec/shoulda/active_model/ensure_length_of_matcher_spec.rb +125 -0
  71. data/spec/shoulda/active_model/helpers_spec.rb +100 -0
  72. data/spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb +43 -0
  73. data/spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb +48 -0
  74. data/spec/shoulda/active_model/validate_format_of_matcher_spec.rb +38 -0
  75. data/spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb +62 -0
  76. data/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb +121 -0
  77. data/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb +143 -0
  78. data/spec/shoulda/active_record/accept_nested_attributes_for_matcher_spec.rb +84 -0
  79. data/spec/shoulda/active_record/association_matcher_spec.rb +449 -0
  80. data/spec/shoulda/active_record/have_db_column_matcher_spec.rb +185 -0
  81. data/spec/shoulda/active_record/have_db_index_matcher_spec.rb +88 -0
  82. data/spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb +46 -0
  83. data/spec/shoulda/active_record/query_the_database_matcher_spec.rb +45 -0
  84. data/spec/shoulda/active_record/serialize_matcher_spec.rb +81 -0
  85. data/spec/spec_helper.rb +31 -0
  86. data/spec/support/model_builder.rb +149 -0
  87. metadata +211 -60
  88. data/CONTRIBUTION_GUIDELINES.rdoc +0 -10
  89. data/README.rdoc +0 -80
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::RenderWithLayoutMatcher do
4
+ include ActionController::TemplateAssertions
5
+
6
+ context "a controller that renders with a layout" do
7
+ let(:controller) { build_response { render :layout => 'wide' } }
8
+
9
+ before do
10
+ create_view('layouts/wide.html.erb', 'some content, <%= yield %>')
11
+ end
12
+
13
+ it "should accept rendering with any layout" do
14
+ controller.should render_with_layout
15
+ end
16
+
17
+ it "should accept rendering with that layout" do
18
+ controller.should render_with_layout(:wide)
19
+ end
20
+
21
+ it "should reject rendering with another layout" do
22
+ controller.should_not render_with_layout(:other)
23
+ end
24
+ end
25
+
26
+ context "a controller that renders without a layout" do
27
+ let(:controller) { build_response { render :layout => false } }
28
+
29
+ it "should reject rendering with a layout" do
30
+ controller.should_not render_with_layout
31
+ end
32
+ end
33
+
34
+ context "given a context with layouts" do
35
+ let(:layout) { 'happy' }
36
+ let(:controller) { build_response { render :layout => false } }
37
+
38
+ before do
39
+ @layouts = Hash.new(0)
40
+ @layouts[layout] = 1
41
+ end
42
+
43
+ it "should accept that layout in that context" do
44
+ controller.should render_with_layout(layout).in_context(self)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::RespondWithContentTypeMatcher do
4
+ context "a controller responding with content type :xml" do
5
+ let(:controller) { build_response { render :xml => { :user => "thoughtbot" }.to_xml } }
6
+
7
+ it "should accept responding with content type :xml" do
8
+ controller.should respond_with_content_type(:xml)
9
+ end
10
+
11
+ it "should accept responding with content type 'application/xml'" do
12
+ controller.should respond_with_content_type('application/xml')
13
+ end
14
+
15
+ it "should accept responding with content type /xml/" do
16
+ controller.should respond_with_content_type(/xml/)
17
+ end
18
+
19
+ it "should reject responding with another content type" do
20
+ controller.should_not respond_with_content_type(:json)
21
+ end
22
+ end
23
+
24
+ it "should generate the correct test name" do
25
+ respond_with_content_type(:xml).description.
26
+ should == "respond with content type of application/xml"
27
+ end
28
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::RespondWithMatcher do
4
+ context "a controller responding with success" do
5
+ let(:controller) { build_response { render :text => "text", :status => 200 } }
6
+
7
+ it "should accept responding with 200" do
8
+ controller.should respond_with(200)
9
+ end
10
+
11
+ it "should accept responding with :success" do
12
+ controller.should respond_with(:success)
13
+ end
14
+
15
+ it "should reject responding with another status" do
16
+ controller.should_not respond_with(:error)
17
+ end
18
+ end
19
+
20
+ context "a controller responding with redirect" do
21
+ let(:controller) { build_response { render :text => "text", :status => 301 } }
22
+
23
+ it "should accept responding with 301" do
24
+ controller.should respond_with(301)
25
+ end
26
+
27
+ it "should accept responding with :redirect" do
28
+ controller.should respond_with(:redirect)
29
+ end
30
+
31
+ it "should reject responding with another status" do
32
+ controller.should_not respond_with(:error)
33
+ end
34
+ end
35
+
36
+ context "a controller responding with missing" do
37
+ let(:controller) { build_response { render :text => "text", :status => 404 } }
38
+
39
+ it "should accept responding with 404" do
40
+ controller.should respond_with(404)
41
+ end
42
+
43
+ it "should accept responding with :missing" do
44
+ controller.should respond_with(:missing)
45
+ end
46
+
47
+ it "should reject responding with another status" do
48
+ controller.should_not respond_with(:success)
49
+ end
50
+ end
51
+
52
+ context "a controller responding with error" do
53
+ let(:controller) { build_response { render :text => "text", :status => 500 } }
54
+
55
+ it "should accept responding with 500" do
56
+ controller.should respond_with(500)
57
+ end
58
+
59
+ it "should accept responding with :error" do
60
+ controller.should respond_with(:error)
61
+ end
62
+
63
+ it "should reject responding with another status" do
64
+ controller.should_not respond_with(:success)
65
+ end
66
+ end
67
+
68
+ context "a controller responding with not implemented" do
69
+ let(:controller) { build_response { render :text => "text", :status => 501 } }
70
+
71
+ it "should accept responding with 501" do
72
+ controller.should respond_with(501)
73
+ end
74
+
75
+ it "should accept responding with :not_implemented" do
76
+ controller.should respond_with(:not_implemented)
77
+ end
78
+
79
+ it "should reject responding with another status" do
80
+ controller.should_not respond_with(:success)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::RouteMatcher do
4
+ context "given a controller with a defined glob url" do
5
+ let(:controller) { define_controller('Examples').new }
6
+
7
+ before do
8
+ define_routes do
9
+ match 'examples/*id', :to => 'examples#example'
10
+ end
11
+ end
12
+
13
+ it "should accept glob route" do
14
+ controller.should route(:get, '/examples/foo/bar').
15
+ to(:action => 'example', :id => 'foo/bar')
16
+ end
17
+ end
18
+
19
+ context "given a controller with a defined route" do
20
+ let!(:controller) { define_controller('Examples').new }
21
+
22
+ before do
23
+ define_routes do
24
+ match 'examples/:id', :to => 'examples#example'
25
+ end
26
+ end
27
+
28
+ it "should accept routing the correct path to the correct parameters" do
29
+ controller.should route(:get, '/examples/1').
30
+ to(:action => 'example', :id => '1')
31
+ end
32
+
33
+ it "should accept a symbol controller" do
34
+ Object.new.should route(:get, '/examples/1').
35
+ to(:controller => :examples,
36
+ :action => 'example',
37
+ :id => '1')
38
+ end
39
+
40
+ it "should accept a symbol action" do
41
+ controller.should route(:get, '/examples/1').
42
+ to(:action => :example, :id => '1')
43
+ end
44
+
45
+ it "should accept a non-string parameter" do
46
+ controller.should route(:get, '/examples/1').
47
+ to(:action => 'example', :id => 1)
48
+ end
49
+
50
+ it "should reject an undefined route" do
51
+ controller.should_not route(:get, '/bad_route').to(:var => 'value')
52
+ end
53
+
54
+ it "should reject a route for another controller" do
55
+ other = define_controller('Other').new
56
+ other.should_not route(:get, '/examples/1').
57
+ to(:action => 'example', :id => '1')
58
+ end
59
+
60
+ it "should reject a route for different parameters" do
61
+ controller.should_not route(:get, '/examples/1').
62
+ to(:action => 'other', :id => '1')
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::SetSessionMatcher do
4
+ context "a controller that sets a session variable" do
5
+ let(:controller) do
6
+ build_response do
7
+ session[:var] = 'value'
8
+ session[:false_var] = false
9
+ end
10
+ end
11
+
12
+ it "should accept assigning to that variable" do
13
+ controller.should set_session(:var)
14
+ end
15
+
16
+ it "should accept assigning the correct value to that variable" do
17
+ controller.should set_session(:var).to('value')
18
+ end
19
+
20
+ it "should reject assigning another value to that variable" do
21
+ controller.should_not set_session(:var).to('other')
22
+ end
23
+
24
+ it "should reject assigning to another variable" do
25
+ controller.should_not set_session(:other)
26
+ end
27
+
28
+ it "should accept assigning nil to another variable" do
29
+ controller.should set_session(:other).to(nil)
30
+ end
31
+
32
+ it "should accept assigning false to that variable" do
33
+ controller.should set_session(:false_var).to(false)
34
+ end
35
+
36
+ it "should accept assigning to the same value in the test context" do
37
+ expected = 'value'
38
+ controller.should set_session(:var).in_context(self).to { expected }
39
+ end
40
+
41
+ it "should reject assigning to the another value in the test context" do
42
+ expected = 'other'
43
+ controller.should_not set_session(:var).in_context(self).to { expected }
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionController::SetTheFlashMatcher do
4
+ context "a controller that sets a flash message" do
5
+ let(:controller) { build_response { flash[:notice] = 'value' } }
6
+
7
+ it "should accept setting any flash message" do
8
+ controller.should set_the_flash
9
+ end
10
+
11
+ it "should accept setting the exact flash message" do
12
+ controller.should set_the_flash.to('value')
13
+ end
14
+
15
+ it "should accept setting a matched flash message" do
16
+ controller.should set_the_flash.to(/value/)
17
+ end
18
+
19
+ it "should reject setting a different flash message" do
20
+ controller.should_not set_the_flash.to('other')
21
+ end
22
+
23
+ it "should reject setting a different pattern" do
24
+ controller.should_not set_the_flash.to(/other/)
25
+ end
26
+ end
27
+
28
+ context "a controller that sets a flash.now message" do
29
+ let(:controller) { build_response { flash.now[:notice] = 'value' } }
30
+
31
+ it "should reject setting any flash message" do
32
+ controller.should_not set_the_flash
33
+ end
34
+
35
+ it "should accept setting any flash.now message" do
36
+ controller.should set_the_flash.now
37
+ end
38
+
39
+ it "should accept setting the exact flash.now message" do
40
+ controller.should set_the_flash.to('value').now
41
+ end
42
+
43
+ it "should accept setting a matched flash.now message" do
44
+ controller.should set_the_flash.to(/value/).now
45
+ end
46
+
47
+ it "should reject setting a different flash.now message" do
48
+ controller.should_not set_the_flash.to('other').now
49
+ end
50
+
51
+ it "should reject setting a different flash.now pattern" do
52
+ controller.should_not set_the_flash.to(/other/).now
53
+ end
54
+ end
55
+
56
+ context "a controller that sets a flash message of notice and alert" do
57
+ let(:controller) do
58
+ build_response do
59
+ flash[:notice] = 'value'
60
+ flash[:alert] = 'other'
61
+ end
62
+ end
63
+
64
+ it "should accept flash message of notice" do
65
+ controller.should set_the_flash[:notice]
66
+ end
67
+
68
+ it "should accept flash message of alert" do
69
+ controller.should set_the_flash[:notice]
70
+ end
71
+
72
+ it "should reject flash message of warning" do
73
+ controller.should_not set_the_flash[:warning]
74
+ end
75
+
76
+ it "should accept exact flash message of notice" do
77
+ controller.should set_the_flash[:notice].to('value')
78
+ end
79
+
80
+ it "should accept setting a matched flash message of notice" do
81
+ controller.should set_the_flash[:notice].to(/value/)
82
+ end
83
+
84
+ it "should reject setting a different flash message of notice" do
85
+ controller.should_not set_the_flash[:notice].to('other')
86
+ end
87
+
88
+ it "should reject setting a different pattern" do
89
+ controller.should_not set_the_flash[:notice].to(/other/)
90
+ end
91
+ end
92
+
93
+ context "a controller that sets multiple flash messages" do
94
+ let(:controller) do
95
+ build_response do
96
+ flash.now[:notice] = 'value'
97
+ flash[:success] = 'great job'
98
+ end
99
+ end
100
+
101
+ it "should accept setting any flash.now message" do
102
+ controller.should set_the_flash.now
103
+ controller.should set_the_flash
104
+ end
105
+
106
+ it "should accept setting a matched flash.now message" do
107
+ controller.should set_the_flash.to(/value/).now
108
+ controller.should set_the_flash.to(/great/)
109
+ end
110
+
111
+ it "should reject setting a different flash.now message" do
112
+ controller.should_not set_the_flash.to('other').now
113
+ controller.should_not set_the_flash.to('other')
114
+ end
115
+ end
116
+
117
+ context "a controller that doesn't set a flash message" do
118
+ let(:controller) { build_response }
119
+
120
+ it "should reject setting any flash message" do
121
+ controller.should_not set_the_flash
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,293 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActionMailer::HaveSentEmailMatcher do
4
+ subject { Shoulda::Matchers::ActionMailer::HaveSentEmailMatcher.new(self) }
5
+
6
+ def add_mail_to_deliveries(params = nil)
7
+ ::ActionMailer::Base.deliveries << Mailer.the_email(params)
8
+ end
9
+
10
+ context "testing with instance variables with no multipart" do
11
+ let(:info) do
12
+ {
13
+ :from => "do-not-reply@example.com",
14
+ :reply_to => "reply-to-me@example.com",
15
+ :to => "myself@me.com",
16
+ :cc => ["you@you.com", "joe@bob.com", "hello@goodbye.com"],
17
+ :bcc => ["test@example.com", "sam@bob.com", "goodbye@hello.com"],
18
+ :subject => "This is spam",
19
+ :body => "Every email is spam."
20
+ }
21
+ end
22
+
23
+ before do
24
+ define_mailer(:mailer, [:the_email]) do
25
+ def the_email(params)
26
+ mail params
27
+ end
28
+ end
29
+ add_mail_to_deliveries(info)
30
+ end
31
+
32
+ after { ::ActionMailer::Base.deliveries.clear }
33
+
34
+ it "should send an e-mail based on subject" do
35
+ should have_sent_email.with_subject{ info[:subject] }
36
+ end
37
+
38
+ it "should send an e-mail based on recipient" do
39
+ should have_sent_email.to(nil) { info[:to] }
40
+ end
41
+
42
+ it "should send an e-mail based on sender" do
43
+ should have_sent_email.from{ info[:from] }
44
+ end
45
+
46
+ it "should send an e-email based on reply_to" do
47
+ should have_sent_email.reply_to { info[:reply_to] }
48
+ end
49
+
50
+ it "should send an e-mail based on cc" do
51
+ should have_sent_email.cc{ info[:cc][0] }
52
+ end
53
+
54
+ it "should send an e-mail based on cc list" do
55
+ should have_sent_email.with_cc{ info[:cc] }
56
+ end
57
+
58
+ it "should send an e-mail based on bcc" do
59
+ should have_sent_email.bcc{ info[:bcc][0] }
60
+ end
61
+
62
+ it "should send an e-mail based on bcc list" do
63
+ should have_sent_email.with_bcc{ info[:bcc] }
64
+ end
65
+
66
+ it "should send an e-mail based on body" do
67
+ should have_sent_email.with_body{ info[:body] }
68
+ end
69
+ end
70
+
71
+ context "testing with instance variables with multiple parts" do
72
+ let(:info) do
73
+ {
74
+ :from => "do-not-reply@example.com",
75
+ :to => "myself@me.com",
76
+ :cc => ["you@you.com", "joe@bob.com", "hello@goodbye.com"],
77
+ :bcc => ["test@example.com", "sam@bob.com", "goodbye@hello.com"],
78
+ :subject => "This is spam",
79
+ :text => "Every email is spam.",
80
+ :html => "<h1>HTML is spam.</h1><p>Notably.</p>"
81
+ }
82
+ end
83
+
84
+ before do
85
+ define_mailer(:mailer, [:the_email]) do
86
+ def the_email(params)
87
+ mail params do |format|
88
+ format.text { render :text => params[:text] }
89
+ format.html { render :text => params[:html] }
90
+ end
91
+ end
92
+ end
93
+ add_mail_to_deliveries(info)
94
+ end
95
+
96
+ after { ::ActionMailer::Base.deliveries.clear }
97
+
98
+ it "should send emails with text and html parts" do
99
+ should have_sent_email.with_part('text/plain') { info[:text] }.with_part('text/html') { info[:html] }
100
+ end
101
+
102
+ it "should have the block override the method argument" do
103
+ should have_sent_email.with_part('text/plain', 'foo') { info[:text] }.with_part('text/html', /bar/) { info[:html] }
104
+ end
105
+ end
106
+
107
+ context "an email without multiple parts" do
108
+ before do
109
+ define_mailer :mailer, [:the_email] do
110
+ def the_email(params)
111
+ mail :from => "do-not-reply@example.com",
112
+ :to => "myself@me.com",
113
+ :subject => "This is spam",
114
+ :cc => ["you@you.com", "joe@bob.com", "hello@goodbye.com"],
115
+ :bcc => ["test@example.com", "sam@bob.com", "goodbye@hello.com"],
116
+ :body => "Every email is spam."
117
+ end
118
+ end
119
+ add_mail_to_deliveries
120
+ end
121
+
122
+ after { ::ActionMailer::Base.deliveries.clear }
123
+
124
+ it "accepts sent-email when it is not multipart" do
125
+ should_not have_sent_email.multipart
126
+ matcher = have_sent_email.multipart(true)
127
+ matcher.matches?(Mailer.the_email(nil))
128
+ matcher.failure_message.should =~ /Expected sent email being multipart/
129
+ end
130
+
131
+ it "matches the body with a regexp" do
132
+ should have_sent_email.with_body(/email is spam/)
133
+ end
134
+
135
+ it "matches the body with a string" do
136
+ should have_sent_email.with_body("Every email is spam.")
137
+ should_not have_sent_email.with_body("emails is")
138
+ end
139
+ end
140
+
141
+ context "an email with both a text/plain and text/html part" do
142
+ before do
143
+ define_mailer :mailer, [:the_email] do
144
+ def the_email(params)
145
+ mail :from => "do-not-reply@example.com",
146
+ :to => "myself@me.com",
147
+ :cc => ["you@you.com", "joe@bob.com", "hello@goodbye.com"],
148
+ :bcc => ["test@example.com", "sam@bob.com", "goodbye@hello.com"],
149
+ :subject => "This is spam" do |format|
150
+
151
+ format.text { render :text => "Every email is spam." }
152
+ format.html { render :text => "<h1>HTML is spam.</h1><p>Notably.</p>" }
153
+ end
154
+ end
155
+ end
156
+ add_mail_to_deliveries
157
+ end
158
+
159
+ after { ::ActionMailer::Base.deliveries.clear }
160
+
161
+ it "accepts sent e-mail based on the subject" do
162
+ should have_sent_email.with_subject(/is spam$/)
163
+ matcher = have_sent_email.with_subject(/totally safe/)
164
+ matcher.matches?(nil)
165
+ matcher.failure_message.should =~ /Expected sent email with subject/
166
+ end
167
+
168
+ it "accepts sent e-mail based on a string sender" do
169
+ should have_sent_email.from('do-not-reply@example.com')
170
+ matcher = have_sent_email.from('you@example.com')
171
+ matcher.matches?(nil)
172
+ matcher.failure_message.should =~ /Expected sent email from/
173
+ end
174
+
175
+ it "accepts sent e-mail based on a regexp sender" do
176
+ should have_sent_email.from(/@example\.com/)
177
+ matcher = have_sent_email.from(/you@/)
178
+ matcher.matches?(nil)
179
+ matcher.failure_message.should =~ /Expected sent email from/
180
+ end
181
+
182
+ it "accepts sent e-mail based on the body" do
183
+ should have_sent_email.with_body(/is spam\./)
184
+ matcher = have_sent_email.with_body(/totally safe/)
185
+ matcher.matches?(nil)
186
+ matcher.failure_message.should =~ /Expected sent email with body/
187
+ end
188
+
189
+ it "accepts sent e-mail based on a text/plain part" do
190
+ should have_sent_email.with_part('text/plain', /is spam\./)
191
+ matcher = have_sent_email.with_part('text/plain', /HTML is spam/)
192
+ matcher.matches?(nil)
193
+ matcher.failure_message.should =~ /Expected sent email with a text\/plain part containing/
194
+ end
195
+
196
+ it "accepts sent e-mail based on a text/html part" do
197
+ should have_sent_email.with_part('text/html', /HTML is spam/)
198
+ matcher = have_sent_email.with_part('text/html', /HTML is not spam\./)
199
+ matcher.matches?(nil)
200
+ matcher.failure_message.should =~ /Expected sent email with a text\/html part containing/
201
+ end
202
+
203
+ it "accept sent e-mail based on the recipient" do
204
+ should have_sent_email.to('myself@me.com')
205
+ matcher = have_sent_email.to('you@example.com')
206
+ matcher.matches?(nil)
207
+ matcher.failure_message.should =~ /Expected sent email to/
208
+ end
209
+
210
+ it "accepts sent e-mail based on cc string" do
211
+ should have_sent_email.cc('joe@bob.com')
212
+ matcher = have_sent_email.cc('you@example.com')
213
+ matcher.matches?(nil)
214
+ matcher.failure_message.should =~ /Expected sent email cc/
215
+ end
216
+
217
+ it "accepts sent-email based on cc regex" do
218
+ should have_sent_email.cc(/@bob\.com/)
219
+ matcher = have_sent_email.cc(/us@/)
220
+ matcher.matches?(nil)
221
+ matcher.failure_message.should =~ /Expected sent email cc/
222
+ end
223
+
224
+ it "accepts sent e-mail based on cc list" do
225
+ should have_sent_email.with_cc(['you@you.com', 'joe@bob.com'])
226
+ matcher = have_sent_email.with_cc(['you@example.com'])
227
+ matcher.matches?(nil)
228
+ matcher.failure_message.should =~ /Expected sent email with cc/
229
+ end
230
+
231
+ it "accepts sent e-mail based on bcc string" do
232
+ should have_sent_email.bcc("goodbye@hello.com")
233
+ matcher = have_sent_email.bcc("test@hello.com")
234
+ matcher.matches?(nil)
235
+ matcher.failure_message.should =~ /Expected sent email bcc/
236
+ end
237
+
238
+ it "accepts sent e-mail based on bcc regex" do
239
+ should have_sent_email.bcc(/@example\.com/)
240
+ matcher = have_sent_email.bcc(/you@/)
241
+ matcher.matches?(nil)
242
+ matcher.failure_message.should =~ /Expected sent email bcc/
243
+ end
244
+
245
+ it "accepts sent e-mail based on bcc list" do
246
+ should have_sent_email.with_bcc(['sam@bob.com', 'test@example.com'])
247
+ matcher = have_sent_email.with_bcc(['you@you.com', 'joe@bob.com'])
248
+ matcher.matches?(nil)
249
+ matcher.failure_message.should =~ /Expected sent email with bcc/
250
+ end
251
+
252
+ it "accepts sent-email when it is multipart" do
253
+ should have_sent_email.multipart
254
+ matcher = have_sent_email.multipart(false)
255
+ matcher.matches?(nil)
256
+ matcher.failure_message.should =~ /Expected sent email not being multipart/
257
+ end
258
+
259
+ it "lists all the deliveries within failure message" do
260
+ add_mail_to_deliveries
261
+
262
+ matcher = have_sent_email.to('you@example.com')
263
+ matcher.matches?(nil)
264
+ matcher.failure_message.should =~ /Deliveries:\n"This is spam" to \["myself@me\.com"\]\n"This is spam" to \["myself@me\.com"\]/
265
+ end
266
+
267
+ it "allows chaining" do
268
+ should have_sent_email.with_subject(/spam/).from('do-not-reply@example.com').with_body(/spam/).
269
+ with_part('text/plain', /is spam\./).with_part('text/html', /HTML is spam/).to('myself@me.com')
270
+ should_not have_sent_email.with_subject(/ham/).from('you@example.com').with_body(/ham/).
271
+ with_part('text/plain', /is ham/).with_part('text/html', /HTML is ham/).to('them@example.com')
272
+ end
273
+ end
274
+
275
+ it "provides a detailed description of the e-mail expected to be sent" do
276
+ matcher = have_sent_email
277
+ matcher.description.should == 'send an email'
278
+ matcher = matcher.with_subject("Welcome!")
279
+ matcher.description.should == 'send an email with a subject of "Welcome!"'
280
+ matcher = matcher.with_body("Welcome, human!")
281
+ matcher.description.should == 'send an email with a subject of "Welcome!" containing "Welcome, human!"'
282
+ matcher = matcher.with_part('text/plain', 'plain')
283
+ matcher.description.should == 'send an email with a subject of "Welcome!" containing "Welcome, human!" having a text/plain part containing "plain"'
284
+ matcher = matcher.with_part('text/html', 'html')
285
+ matcher.description.should == 'send an email with a subject of "Welcome!" containing "Welcome, human!" having a text/plain part containing "plain" having a text/html part containing "html"'
286
+ matcher = matcher.from("alien@example.com")
287
+ matcher.description.should == 'send an email with a subject of "Welcome!" containing "Welcome, human!" having a text/plain part containing "plain" having a text/html part containing "html" from "alien@example.com"'
288
+ matcher = matcher.to("human@example.com")
289
+ matcher.description.should == 'send an email with a subject of "Welcome!" containing "Welcome, human!" having a text/plain part containing "plain" having a text/html part containing "html" from "alien@example.com" to "human@example.com"'
290
+ matcher = matcher.reply_to("reply-to-me@example.com")
291
+ matcher.description.should == 'send an email with a subject of "Welcome!" containing "Welcome, human!" having a text/plain part containing "plain" having a text/html part containing "html" from "alien@example.com" reply to "reply-to-me@example.com" to "human@example.com"'
292
+ end
293
+ end