minitest-rails 3.0.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE +1 -1
  4. data/README.md +94 -0
  5. data/UPDATING.md +31 -0
  6. data/lib/generators/minitest.rb +17 -4
  7. data/lib/generators/minitest/controller/controller_generator.rb +6 -9
  8. data/lib/generators/minitest/controller/templates/{controller_spec.rb → functional_spec.rb.tt} +4 -4
  9. data/lib/generators/minitest/controller/templates/{controller_test.rb → functional_test.rb.tt} +3 -3
  10. data/lib/generators/minitest/generator/generator_generator.rb +3 -8
  11. data/lib/generators/minitest/generator/templates/{generator_spec.rb → generator_spec.rb.tt} +2 -2
  12. data/lib/generators/minitest/generator/templates/{generator_test.rb → generator_test.rb.tt} +1 -2
  13. data/lib/generators/minitest/helper/helper_generator.rb +5 -3
  14. data/lib/generators/minitest/install/install_generator.rb +19 -8
  15. data/lib/generators/minitest/install/templates/test/test_helper.rb.tt +16 -0
  16. data/lib/generators/minitest/integration/integration_generator.rb +12 -9
  17. data/lib/generators/minitest/integration/templates/integration_spec.rb.tt +9 -0
  18. data/lib/generators/minitest/integration/templates/integration_test.rb.tt +9 -0
  19. data/lib/generators/minitest/job/job_generator.rb +13 -10
  20. data/lib/generators/minitest/job/templates/unit_spec.rb.tt +9 -0
  21. data/lib/generators/minitest/job/templates/{job_test.rb → unit_test.rb.tt} +3 -3
  22. data/lib/generators/minitest/mailer/mailer_generator.rb +8 -11
  23. data/lib/generators/minitest/mailer/templates/functional_spec.rb.tt +21 -0
  24. data/lib/generators/minitest/mailer/templates/{mailer_test.rb → functional_test.rb.tt} +3 -3
  25. data/lib/generators/minitest/mailer/templates/{preview.rb → preview.rb.tt} +0 -0
  26. data/lib/generators/minitest/model/model_generator.rb +9 -12
  27. data/lib/generators/minitest/model/templates/{fixtures.yml → fixtures.yml.tt} +3 -3
  28. data/lib/generators/minitest/model/templates/unit_spec.rb.tt +9 -0
  29. data/lib/generators/minitest/model/templates/unit_test.rb.tt +9 -0
  30. data/lib/generators/minitest/plugin/plugin_generator.rb +15 -0
  31. data/lib/generators/minitest/plugin/templates/%file_name%_test.rb.tt +15 -0
  32. data/lib/generators/minitest/plugin/templates/test_helper.rb +2 -0
  33. data/lib/generators/minitest/scaffold/scaffold_generator.rb +21 -23
  34. data/lib/generators/minitest/scaffold/templates/api_functional_spec.rb.tt +42 -0
  35. data/lib/generators/minitest/scaffold/templates/api_functional_test.rb.tt +44 -0
  36. data/lib/generators/minitest/scaffold/templates/functional_spec.rb.tt +52 -0
  37. data/lib/generators/minitest/scaffold/templates/{controller_test.rb → functional_test.rb.tt} +16 -16
  38. data/lib/minitest/rails.rb +45 -108
  39. data/lib/minitest/rails/assertions.rb +5 -906
  40. data/lib/minitest/rails/assertions/action_dispatch.rb +356 -0
  41. data/lib/minitest/rails/assertions/action_mailer.rb +115 -0
  42. data/lib/minitest/rails/assertions/action_view.rb +356 -0
  43. data/lib/minitest/rails/assertions/active_job.rb +151 -0
  44. data/lib/minitest/rails/assertions/active_support.rb +29 -0
  45. data/lib/minitest/rails/expectations.rb +4 -565
  46. data/lib/minitest/rails/expectations/action_dispatch.rb +307 -0
  47. data/lib/minitest/rails/expectations/action_mailer.rb +136 -0
  48. data/lib/minitest/rails/expectations/active_job.rb +182 -0
  49. data/lib/minitest/rails/expectations/active_support.rb +42 -0
  50. data/lib/minitest/rails/railtie.rb +3 -2
  51. data/lib/minitest/rails/version.rb +1 -1
  52. metadata +72 -89
  53. data/.autotest +0 -23
  54. data/.gemtest +0 -0
  55. data/.travis.yml +0 -25
  56. data/CHANGELOG.rdoc +0 -223
  57. data/Gemfile +0 -7
  58. data/Manifest.txt +0 -76
  59. data/README.rdoc +0 -110
  60. data/Rakefile +0 -30
  61. data/gemfiles/5.0.gemfile +0 -5
  62. data/gemfiles/head.gemfile +0 -5
  63. data/lib/generators/.document +0 -0
  64. data/lib/generators/minitest/install/templates/test_helper.rb +0 -19
  65. data/lib/generators/minitest/integration/templates/integration_spec.rb +0 -8
  66. data/lib/generators/minitest/integration/templates/integration_test.rb +0 -7
  67. data/lib/generators/minitest/job/templates/job_spec.rb +0 -9
  68. data/lib/generators/minitest/mailer/templates/mailer_spec.rb +0 -20
  69. data/lib/generators/minitest/model/templates/model_spec.rb +0 -11
  70. data/lib/generators/minitest/model/templates/model_test.rb +0 -13
  71. data/lib/generators/minitest/scaffold/templates/api_controller_spec.rb +0 -42
  72. data/lib/generators/minitest/scaffold/templates/api_controller_test.rb +0 -44
  73. data/lib/generators/minitest/scaffold/templates/controller_spec.rb +0 -52
  74. data/lib/minitest/rails/constant_lookup.rb +0 -52
  75. data/lib/minitest/rails/controller.rb +0 -14
  76. data/minitest-rails.gemspec +0 -48
  77. data/tasks/test.rake +0 -29
  78. data/test/generators/test_controller_generator.rb +0 -58
  79. data/test/generators/test_generator_generator.rb +0 -58
  80. data/test/generators/test_helper_generator.rb +0 -34
  81. data/test/generators/test_install_generator.rb +0 -27
  82. data/test/generators/test_job_generator.rb +0 -45
  83. data/test/generators/test_mailer_generator.rb +0 -42
  84. data/test/generators/test_model_generator.rb +0 -80
  85. data/test/generators/test_scaffold_generator.rb +0 -26
  86. data/test/helper.rb +0 -93
  87. data/test/rails/action_dispatch/test_assertions.rb +0 -39
  88. data/test/rails/action_dispatch/test_expectations.rb +0 -39
  89. data/test/rails/action_dispatch/test_spec_type.rb +0 -49
  90. data/test/rails/action_mailer/test_mailers.rb +0 -109
  91. data/test/rails/action_mailer/test_spec_type.rb +0 -47
  92. data/test/rails/action_view/test_helpers.rb +0 -73
  93. data/test/rails/action_view/test_spec_type.rb +0 -58
  94. data/test/rails/active_job/test_assertions.rb +0 -46
  95. data/test/rails/active_job/test_expectations.rb +0 -55
  96. data/test/rails/active_job/test_spec_type.rb +0 -49
  97. data/test/rails/active_support/test_assertions.rb +0 -31
  98. data/test/rails/active_support/test_expectations.rb +0 -21
  99. data/test/rails/active_support/test_spec_type.rb +0 -28
  100. data/test/rails/generators/test_spec_type.rb +0 -45
  101. data/test/rails/test_constant_lookup.rb +0 -58
  102. data/test/test_sanity.rb +0 -7
@@ -0,0 +1,307 @@
1
+ require "active_support/concern"
2
+ require "minitest/rails/expectations/active_support"
3
+
4
+ module Minitest
5
+ module Rails
6
+ module Expectations
7
+ # This exists as a module to allow easy mixing into classes other than
8
+ # ActionDispatch::IntegrationTest where you might want to do job testing.
9
+ module ActionDispatch
10
+ ##
11
+ # Expects that the provided options can be used to generate the provided path. This is the inverse of +assert_recognizes+.
12
+ # The +extras+ parameter is used to tell the request the names and values of additional request parameters that would be in
13
+ # a query string. The +message+ parameter allows you to specify a custom error message for assertion failures.
14
+ #
15
+ # The +defaults+ parameter is unused.
16
+ #
17
+ # # Expects that the default action is generated for a route with no action
18
+ # value({controller: "items", action: "index"}).must_route_from "/items"
19
+ #
20
+ # # Tests that the list action is properly routed
21
+ # value({controller: "items", action: "list"}).must_route_to "/items/list"
22
+ #
23
+ # # Tests the generation of a route with a parameter
24
+ # value({ controller: "items", action: "list", id: "1" }).must_route_from "/items/list/1"
25
+ #
26
+ # # Expects that the generated route gives us our custom route
27
+ # value({ controller: 'scm', action: 'show_diff', revision: "12" }).must_route_from "changesets/12"
28
+ #
29
+ # See also ActionView::TestCase#assert_generates
30
+ # See also ActionDispatch::IntegrationTest#assert_generates
31
+ #
32
+ # :method: must_route_from
33
+ # :call-seq: options.must_route_from(expected_path, defaults={}, extras = {}, message=nil)
34
+ infect_an_assertion :assert_generates, :must_route_to
35
+
36
+ ##
37
+ # Expects that the routing of the given +path+ was handled correctly and that the parsed options (given in the +expected_options+ hash)
38
+ # match +path+. Basically, it asserts that \Rails recognizes the route given by +expected_options+.
39
+ #
40
+ # Pass a hash in the second argument (+path+) to specify the request method. This is useful for routes
41
+ # requiring a specific HTTP method. The hash should contain a :path with the incoming request path
42
+ # and a :method containing the required HTTP verb.
43
+ #
44
+ # # assert that POSTing to /items will call the create action on ItemsController
45
+ # assert_recognizes({controller: 'items', action: 'create'}, {path: 'items', method: :post})
46
+ #
47
+ # You can also pass in +extras+ with a hash containing URL parameters that would normally be in the query string. This can be used
48
+ # to assert that values in the query string string will end up in the params hash correctly. To test query strings you must use the
49
+ # extras argument, appending the query string on the path directly will not work. For example:
50
+ #
51
+ # # Expect that a path of '/items/list/1?view=print' returns the correct options
52
+ # value('items/list/1').must_route_from({controller: 'items', action: 'list', id: '1', view: 'print'}, { view: "print" })
53
+ #
54
+ # The +message+ parameter allows you to pass in an error message that is displayed upon failure.
55
+ #
56
+ # # Check the default route (i.e., the index action)
57
+ # value('items').must_route_from({controller: 'items', action: 'index'})
58
+ #
59
+ # # Test a specific action
60
+ # value('items/list').must_route_from({controller: 'items', action: 'list'})
61
+ #
62
+ # # Test an action with a parameter
63
+ # value('items/destroy/1').must_route_from({controller: 'items', action: 'destroy', id: '1'})
64
+ #
65
+ # # Test a custom route
66
+ # value('view/item1').must_route_from({controller: 'items', action: 'show', id: '1'})
67
+ #
68
+ # See also ActionView::TestCase#assert_recognizes
69
+ # See also ActionDispatch::IntegrationTest#assert_recognizes
70
+ #
71
+ # :method: must_route_from
72
+ # :call-seq: path.must_route_from(expected_options, extras={}, msg=nil)
73
+ infect_an_assertion :assert_recognizes, :must_route_from
74
+
75
+ ##
76
+ # Expects that path and options match both ways; in other words, it verifies that <tt>path</tt> generates
77
+ # <tt>options</tt> and then that <tt>options</tt> generates <tt>path</tt>. This essentially combines +assert_recognizes+
78
+ # and +assert_generates+ into one step.
79
+ #
80
+ # The +extras+ hash allows you to specify options that would normally be provided as a query string to the action. The
81
+ # +message+ parameter allows you to specify a custom error message to display upon failure.
82
+ #
83
+ # # Expect a basic route: a controller with the default action (index)
84
+ # value({ controller: 'home', action: 'index' }).must_route_for '/home'
85
+ #
86
+ # # Test a route generated with a specific controller, action, and parameter (id)
87
+ # value({ controller: 'entries', action: 'show', id: 23 }).must_route_for '/entries/show/23'
88
+ #
89
+ # # Expect a basic route (controller + default action), with an error message if it fails
90
+ # value({ controller: 'store', action: 'index' }).must_route_for '/store'
91
+ #
92
+ # # Tests a route, providing a defaults hash
93
+ # value({id: "9", item: "square"}).must_route_for 'controller/action/9', {controller: "controller", action: "action"}, {}, {item: "square"}
94
+ #
95
+ # # Tests a route with a HTTP method
96
+ # value({ controller: "product", action: "update", id: "321" }).must_route_for({ method: 'put', path: '/product/321' })
97
+ #
98
+ # See also ActionView::TestCase#assert_routing
99
+ # See also ActionDispatch::IntegrationTest#assert_routing
100
+ #
101
+ # :method: must_route_for
102
+ # :call-seq: options.must_route_for(path, defaults={}, extras={}, message=nil)
103
+ infect_an_assertion :assert_routing, :must_route_for
104
+
105
+ # An expectation that selects elements and makes one or more equality tests.
106
+ #
107
+ # If the first argument is an element, selects all matching elements
108
+ # starting from (and including) that element and all its children in
109
+ # depth-first order.
110
+ #
111
+ # If no element if specified, calling +must_select+ selects from the
112
+ # response HTML unless +must_select+ is called from within an +must_select+ block.
113
+ #
114
+ # When called with a block +must_select+ passes an array of selected elements
115
+ # to the block. Calling +must_select+ from the block, with no element specified,
116
+ # runs the expectation on the complete set of elements selected by the enclosing expectation.
117
+ # Alternatively the array may be iterated through so that +must_select+ can be called
118
+ # separately for each element.
119
+ #
120
+ #
121
+ # ==== Example
122
+ # If the response contains two ordered lists, each with four list elements then:
123
+ # must_select "ol" do |elements|
124
+ # elements.each do |element|
125
+ # must_select element, "li", 4
126
+ # end
127
+ # end
128
+ #
129
+ # will pass, as will:
130
+ # must_select "ol" do
131
+ # must_select "li", 8
132
+ # end
133
+ #
134
+ # The selector may be a CSS selector expression (String), an expression
135
+ # with substitution values, or an HTML::Selector object.
136
+ #
137
+ # === Equality Tests
138
+ #
139
+ # The equality test may be one of the following:
140
+ # * <tt>true</tt> - Assertion is true if at least one element selected.
141
+ # * <tt>false</tt> - Assertion is true if no element selected.
142
+ # * <tt>String/Regexp</tt> - Assertion is true if the text value of at least
143
+ # one element matches the string or regular expression.
144
+ # * <tt>Integer</tt> - Assertion is true if exactly that number of
145
+ # elements are selected.
146
+ # * <tt>Range</tt> - Assertion is true if the number of selected
147
+ # elements fit the range.
148
+ # If no equality test specified, the expectation is true if at least one
149
+ # element selected.
150
+ #
151
+ # To perform more than one equality tests, use a hash with the following keys:
152
+ # * <tt>:text</tt> - Narrow the selection to elements that have this text
153
+ # value (string or regexp).
154
+ # * <tt>:html</tt> - Narrow the selection to elements that have this HTML
155
+ # content (string or regexp).
156
+ # * <tt>:count</tt> - Assertion is true if the number of selected elements
157
+ # is equal to this value.
158
+ # * <tt>:minimum</tt> - Assertion is true if the number of selected
159
+ # elements is at least this value.
160
+ # * <tt>:maximum</tt> - Assertion is true if the number of selected
161
+ # elements is at most this value.
162
+ #
163
+ # If the method is called with a block, once all equality tests are
164
+ # evaluated the block is called with an array of all matched elements.
165
+ #
166
+ # # At least one form element
167
+ # must_select "form"
168
+ #
169
+ # # Form element includes four input fields
170
+ # must_select "form input", 4
171
+ #
172
+ # # Page title is "Welcome"
173
+ # must_select "title", "Welcome"
174
+ #
175
+ # # Page title is "Welcome" and there is only one title element
176
+ # must_select "title", {count: 1, text: "Welcome"},
177
+ # "Wrong title or more than one title element"
178
+ #
179
+ # # Page contains no forms
180
+ # must_select "form", false, "This page must contain no forms"
181
+ #
182
+ # # Test the content and style
183
+ # must_select "body div.header ul.menu"
184
+ #
185
+ # # Use substitution values
186
+ # must_select "ol>li#?", /item-\d+/
187
+ #
188
+ # # All input fields in the form have a name
189
+ # must_select "form input" do
190
+ # must_select "[name=?]", /.+/ # Not empty
191
+ # end
192
+ #
193
+ # See also ActionView::TestCase#assert_select
194
+ # See also ActionDispatch::IntegrationTest#assert_select
195
+ #
196
+ # :method: must_select
197
+ # :call-seq: must_select(*args, &block)
198
+
199
+ # Extracts the body of an email and runs nested expectations on it.
200
+ #
201
+ # You must enable deliveries for this expectation to work, use:
202
+ # ActionMailer::Base.perform_deliveries = true
203
+ #
204
+ # must_select_email do
205
+ # must_select "h1", "Email alert"
206
+ # end
207
+ #
208
+ # must_select_email do
209
+ # items = must_select "ol>li"
210
+ # items.each do
211
+ # # Work with items here...
212
+ # end
213
+ # end
214
+ #
215
+ # See also ActionView::TestCase#assert_select_email
216
+ # See also ActionDispatch::IntegrationTest#assert_select_email
217
+ #
218
+ # :method: must_select_email
219
+ # :call-seq: must_select_email(&block)
220
+
221
+ # Extracts the content of an element, treats it as encoded HTML and runs
222
+ # nested expectation on it.
223
+ #
224
+ # You typically call this method within another expectation to operate on
225
+ # all currently selected elements. You can also pass an element or array
226
+ # of elements.
227
+ #
228
+ # The content of each element is un-encoded, and wrapped in the root
229
+ # element +encoded+. It then calls the block with all un-encoded elements.
230
+ #
231
+ # # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
232
+ # must_select "feed[xmlns='http://www.w3.org/2005/Atom']" do
233
+ # # Select each entry item and then the title item
234
+ # must_select "entry>title" do
235
+ # # Run expectations on the encoded title elements
236
+ # must_select_encoded do
237
+ # must_select "b"
238
+ # end
239
+ # end
240
+ # end
241
+ #
242
+ #
243
+ # # Selects all paragraph tags from within the description of an RSS feed
244
+ # must_select "rss[version=2.0]" do
245
+ # # Select description element of each feed item.
246
+ # must_select "channel>item>description" do
247
+ # # Run expectations on the encoded elements.
248
+ # must_select_encoded do
249
+ # must_select "p"
250
+ # end
251
+ # end
252
+ # end
253
+ #
254
+ # See also ActionView::TestCase#assert_select_encoded
255
+ # See also ActionDispatch::IntegrationTest#assert_select_encoded
256
+ #
257
+ # :method: must_select_encoded
258
+ # :call-seq: must_select_encoded(element = nil, &block)
259
+
260
+ ##
261
+ # Checks that two HTML strings are equivalent. That they contain the same elements and attributes with the associated values.
262
+ # Checks the numeric difference between the return value of an expression as a result of what is evaluated.
263
+ #
264
+ # apple_link = '<a href="http://www.example.com">Apples</a>'
265
+ # value(link_to("Apples", "http://www.example.com")).must_dom_equal apple_link
266
+ #
267
+ # See also ActionView::TestCase#assert_dom_equal
268
+ # See also ActionDispatch::IntegrationTest#assert_dom_equal
269
+ #
270
+ # :method: must_dom_equal
271
+ # :args: expected, message = nil
272
+ infect_an_assertion :assert_dom_equal, :must_dom_equal
273
+
274
+ ##
275
+ # Checks that the numeric result of evaluating an expression is not changed before and after invoking.
276
+ #
277
+ # orange_link = '<a href="http://www.example.com">Oranges</a>'
278
+ # link_to("Apples", "http://www.example.com").wont_dom_equal orange_link
279
+ #
280
+ # See also ActionView::TestCase#refute_dom_equal
281
+ # See also ActionDispatch::IntegrationTest#refute_dom_equal
282
+ # See also ActionView::TestCase#assert_dom_not_equal
283
+ # See also ActionDispatch::IntegrationTest#assert_dom_not_equal
284
+ #
285
+ # :method: wont_dom_equal
286
+ # :args: expected, message = nil
287
+ infect_an_assertion :refute_dom_equal, :wont_dom_equal
288
+
289
+ extend ::ActiveSupport::Concern
290
+
291
+ included do
292
+ alias_method :must_respond_with, :assert_response
293
+ alias_method :must_redirect_to, :assert_redirected_to
294
+ alias_method :must_select, :assert_select
295
+ alias_method :must_select_email, :assert_select_email
296
+ alias_method :must_select_encoded, :assert_select_encoded
297
+ end
298
+ end
299
+ end
300
+ end
301
+ end
302
+
303
+ unless ENV["MT_NO_EXPECTATIONS"]
304
+ class ActionDispatch::IntegrationTest # :nodoc:
305
+ include Minitest::Rails::Expectations::ActionDispatch
306
+ end
307
+ end
@@ -0,0 +1,136 @@
1
+ require "active_support/concern"
2
+
3
+ module Minitest
4
+ module Rails
5
+ module Expectations
6
+ module ActionMailer
7
+ ##
8
+ # Asserts that the number of emails sent matches the given number.
9
+ #
10
+ # def test_emails
11
+ # must_have_emails 0
12
+ # ContactMailer.welcome.deliver_now
13
+ # must_have_emails 1
14
+ # ContactMailer.welcome.deliver_now
15
+ # must_have_emails 2
16
+ # end
17
+ #
18
+ # If a block is passed, that block should cause the specified number of
19
+ # emails to be sent.
20
+ #
21
+ # def test_emails_again
22
+ # must_have_emails 1 do
23
+ # ContactMailer.welcome.deliver_now
24
+ # end
25
+ #
26
+ # must_have_emails 2 do
27
+ # ContactMailer.welcome.deliver_now
28
+ # ContactMailer.welcome.deliver_later
29
+ # end
30
+ # end
31
+ #
32
+ # See also ActionMailer::TestClass#assert_emails
33
+ # See https://api.rubyonrails.org/v5.0/classes/ActionMailer/TestHelper.html#method-i-assert_emails
34
+ #
35
+ # :method: must_have_emails
36
+ # :call-seq: must_have_emails(number, &block)
37
+
38
+ # Asserts that no emails have been sent.
39
+ #
40
+ # def test_emails
41
+ # wont_have_emails
42
+ # ContactMailer.welcome.deliver_now
43
+ # must_have_emails 1
44
+ # end
45
+ #
46
+ # If a block is passed, that block should not cause any emails to be sent.
47
+ #
48
+ # def test_emails_again
49
+ # wont_have_emails do
50
+ # # No emails should be sent from this block
51
+ # end
52
+ # end
53
+ #
54
+ # Note: This assertion is simply a shortcut for:
55
+ #
56
+ # must_have_emails 0, &block
57
+ #
58
+ # See also ActionMailer::TestClass#wont_have_emails
59
+ # See https://api.rubyonrails.org/v5.0/classes/ActionMailer/TestHelper.html#method-i-assert_no_emails
60
+ #
61
+ # :method: wont_have_emails
62
+ # :call-seq: wont_have_emails(&block)
63
+
64
+ ##
65
+ # Asserts that the number of emails enqueued for later delivery matches
66
+ # the given number.
67
+ #
68
+ # def test_emails
69
+ # must_have_enqueued_emails 0
70
+ # ContactMailer.welcome.deliver_later
71
+ # must_have_enqueued_emails 1
72
+ # ContactMailer.welcome.deliver_later
73
+ # must_have_enqueued_emails 2
74
+ # end
75
+ #
76
+ # If a block is passed, that block should cause the specified number of
77
+ # emails to be enqueued.
78
+ #
79
+ # def test_emails_again
80
+ # must_have_enqueued_emails 1 do
81
+ # ContactMailer.welcome.deliver_later
82
+ # end
83
+ #
84
+ # must_have_enqueued_emails 2 do
85
+ # ContactMailer.welcome.deliver_later
86
+ # ContactMailer.welcome.deliver_later
87
+ # end
88
+ # end
89
+ #
90
+ # See also ActionMailer::TestClass#assert_enqueued_emails
91
+ # See https://api.rubyonrails.org/v5.0/classes/ActionMailer/TestHelper.html#method-i-assert_enqueued_emails
92
+ #
93
+ # :method: must_have_enqueued_emails
94
+ # :call-seq: must_have_enqueued_emails(number, &block)
95
+
96
+ ##
97
+ # Asserts that no emails are enqueued for later delivery.
98
+ #
99
+ # def test_no_emails
100
+ # wont_have_enqueued_emails
101
+ # ContactMailer.welcome.deliver_later
102
+ # wont_have_enqueued_emails 1
103
+ # end
104
+ #
105
+ # If a block is provided, it should not cause any emails to be enqueued.
106
+ #
107
+ # def test_no_emails
108
+ # wont_have_enqueued_emails do
109
+ # # No emails should be enqueued from this block
110
+ # end
111
+ # end
112
+ #
113
+ # See also ActionMailer::TestClass#assert_no_enqueued_emails
114
+ # See https://api.rubyonrails.org/v5.0/classes/ActionMailer/TestHelper.html#method-i-assert_no_enqueued_emails
115
+ #
116
+ # :method: wont_have_enqueued_emails
117
+ # :call-seq: wont_have_enqueued_emails(&block)
118
+
119
+ extend ::ActiveSupport::Concern
120
+
121
+ included do
122
+ alias_method :must_have_emails, :assert_emails
123
+ alias_method :wont_have_emails, :assert_no_emails
124
+ alias_method :must_have_enqueued_emails, :assert_enqueued_emails
125
+ alias_method :wont_have_enqueued_emails, :assert_no_enqueued_emails
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ unless ENV["MT_NO_EXPECTATIONS"]
133
+ class ActionMailer::TestCase # :nodoc:
134
+ include Minitest::Rails::Expectations::ActionMailer
135
+ end
136
+ end
@@ -0,0 +1,182 @@
1
+ require "active_support/concern"
2
+
3
+ module Minitest
4
+ module Rails
5
+ module Expectations
6
+ # This exists as a module to allow easy mixing into classes
7
+ # other than ActiveJob::TestCase where you might want to do
8
+ # job testing e.g. in an Active Record model which triggers
9
+ # jobs in a callback.
10
+ module ActiveJob
11
+ ##
12
+ # Expects that the number of enqueued jobs matches the given number.
13
+ #
14
+ # def test_jobs
15
+ # must_enqueue_jobs 0
16
+ # HelloJob.perform_later('david')
17
+ # must_enqueue_jobs 1
18
+ # HelloJob.perform_later('abdelkader')
19
+ # must_enqueue_jobs 2
20
+ # end
21
+ #
22
+ # If a block is passed, that block should cause the specified number of
23
+ # jobs to be enqueued.
24
+ #
25
+ # def test_jobs_again
26
+ # must_enqueue_jobs 1 do
27
+ # HelloJob.perform_later('cristian')
28
+ # end
29
+ #
30
+ # must_enqueue_jobs 2 do
31
+ # HelloJob.perform_later('aaron')
32
+ # HelloJob.perform_later('rafael')
33
+ # end
34
+ # end
35
+ #
36
+ # See also ActiveJob::TestCase#assert_enqueued_jobs
37
+ #
38
+ # :method: must_enqueue_jobs
39
+ # :call-seq: must_enqueue_jobs(number)
40
+
41
+ ##
42
+ # Expects that no jobs have been enqueued.
43
+ #
44
+ # def test_jobs
45
+ # wont_enqueue_jobs
46
+ # HelloJob.perform_later('jeremy')
47
+ # must_enqueue_jobs 1
48
+ # end
49
+ #
50
+ # If a block is passed, that block should not cause any job to be enqueued.
51
+ #
52
+ # def test_jobs_again
53
+ # wont_enqueue_jobs do
54
+ # # No job should be enqueued from this block
55
+ # end
56
+ # end
57
+ #
58
+ # Note: This expectation is simply a shortcut for:
59
+ #
60
+ # must_enqueue_jobs 0, &block
61
+ #
62
+ # See also ActiveJob::TestCase#refute_enqueued_jobs
63
+ #
64
+ # :method: wont_enqueue_jobs
65
+ # :call-seq: wont_enqueue_jobs(number)
66
+
67
+ ##
68
+ # Expects that the number of performed jobs matches the given number.
69
+ # If no block is passed, <tt>perform_enqueued_jobs</tt>d
70
+ # must be called around the job call.
71
+ #
72
+ # def test_jobs
73
+ # must_perform_jobs 0
74
+ #
75
+ # perform_enqueued_jobs do
76
+ # HelloJob.perform_later('xavier')
77
+ # end
78
+ # must_perform_jobs 1
79
+ #
80
+ # perform_enqueued_jobs do
81
+ # HelloJob.perform_later('yves')
82
+ # must_perform_jobs 2
83
+ # end
84
+ # end
85
+ #
86
+ # If a block is passed, that block should cause the specified number of
87
+ # jobs to be performed.
88
+ #
89
+ # def test_jobs_again
90
+ # must_perform_jobs 1 do
91
+ # HelloJob.perform_later('robin')
92
+ # end
93
+ #
94
+ # must_perform_jobs 2 do
95
+ # HelloJob.perform_later('carlos')
96
+ # HelloJob.perform_later('sean')
97
+ # end
98
+ # end
99
+ #
100
+ # See also ActiveJob::TestCase#assert_performed_jobs
101
+ #
102
+ # :method: must_perform_jobs
103
+ # :call-seq: must_perform_jobs(number)
104
+
105
+ ##
106
+ # Expects that no jobs have been performed.
107
+ #
108
+ # def test_jobs
109
+ # wont_perform_jobs
110
+ #
111
+ # perform_enqueued_jobs do
112
+ # HelloJob.perform_later('matthew')
113
+ # must_perform_jobs 1
114
+ # end
115
+ # end
116
+ #
117
+ # If a block is passed, that block should not cause any job to be performed.
118
+ #
119
+ # def test_jobs_again
120
+ # wont_perform_jobs do
121
+ # # No job should be performed from this block
122
+ # end
123
+ # end
124
+ #
125
+ # Note: This assertion is simply a shortcut for:
126
+ #
127
+ # must_perform_jobs 0, &block
128
+ #
129
+ # See also ActiveJob::TestCase#refute_performed_jobs
130
+ #
131
+ # :method: wont_perform_jobs
132
+ # :call-seq: wont_perform_jobs(number)
133
+
134
+ ##
135
+ # Expects that the job passed in the block has been enqueued with the given arguments.
136
+ #
137
+ # def test_must_enqueue_with
138
+ # must_enqueue_with(job: MyJob, args: [1,2,3], queue: 'low') do
139
+ # MyJob.perform_later(1,2,3)
140
+ # end
141
+ # end
142
+ #
143
+ # See also Minitest::Rails::Expectations#assert_enqueued_with
144
+ #
145
+ # :method: must_enqueue_with
146
+ # :call-seq: must_enqueue_with(args)
147
+
148
+ ##
149
+ # Expects that the job passed in the block has been performed with the given arguments.
150
+ #
151
+ # def test_must_perform_with
152
+ # must_perform_with(job: MyJob, args: [1,2,3], queue: 'high') do
153
+ # MyJob.perform_later(1,2,3)
154
+ # end
155
+ # end
156
+ #
157
+ # See also Minitest::Rails::Expectations#assert_performed_with
158
+ #
159
+ # :method: must_perform_with
160
+ # :call-seq: must_perform_with(args)
161
+
162
+ extend ::ActiveSupport::Concern
163
+ include ::ActiveJob::TestHelper
164
+
165
+ included do
166
+ alias_method :must_enqueue_jobs, :assert_enqueued_jobs
167
+ alias_method :must_perform_jobs, :assert_performed_jobs
168
+ alias_method :wont_enqueue_jobs, :assert_no_enqueued_jobs
169
+ alias_method :wont_perform_jobs, :assert_no_performed_jobs
170
+ alias_method :must_enqueue_with, :assert_enqueued_with
171
+ alias_method :must_perform_with, :assert_performed_with
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ unless ENV["MT_NO_EXPECTATIONS"]
179
+ class ActiveJob::TestCase # :nodoc:
180
+ include Minitest::Rails::Expectations::ActiveJob
181
+ end
182
+ end