rspec-rails 1.3.2 → 6.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/.document +4 -6
  4. data/.yardopts +11 -0
  5. data/Capybara.md +28 -0
  6. data/Changelog.md +1286 -0
  7. data/LICENSE.md +25 -0
  8. data/README.md +381 -0
  9. data/lib/generators/rspec/channel/channel_generator.rb +12 -0
  10. data/lib/generators/rspec/channel/templates/channel_spec.rb.erb +7 -0
  11. data/lib/generators/rspec/controller/controller_generator.rb +51 -0
  12. data/lib/generators/rspec/controller/templates/controller_spec.rb +16 -0
  13. data/lib/generators/rspec/controller/templates/request_spec.rb +19 -0
  14. data/lib/generators/rspec/controller/templates/routing_spec.rb +13 -0
  15. data/lib/generators/rspec/controller/templates/view_spec.rb +5 -0
  16. data/lib/generators/rspec/feature/feature_generator.rb +29 -0
  17. data/lib/generators/rspec/feature/templates/feature_singular_spec.rb +5 -0
  18. data/lib/generators/rspec/feature/templates/feature_spec.rb +5 -0
  19. data/lib/generators/rspec/generator/generator_generator.rb +24 -0
  20. data/lib/generators/rspec/generator/templates/generator_spec.rb +5 -0
  21. data/lib/generators/rspec/helper/helper_generator.rb +16 -0
  22. data/lib/generators/rspec/helper/templates/helper_spec.rb +17 -0
  23. data/lib/generators/rspec/install/install_generator.rb +80 -0
  24. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +84 -0
  25. data/lib/generators/rspec/integration/integration_generator.rb +29 -0
  26. data/lib/generators/rspec/job/job_generator.rb +13 -0
  27. data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
  28. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  29. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  30. data/lib/generators/rspec/mailer/mailer_generator.rb +30 -0
  31. data/lib/generators/rspec/mailer/templates/fixture +3 -0
  32. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +25 -0
  33. data/lib/generators/rspec/mailer/templates/preview.rb +13 -0
  34. data/lib/generators/rspec/model/model_generator.rb +37 -0
  35. data/lib/generators/rspec/model/templates/fixtures.yml +19 -0
  36. data/lib/generators/rspec/model/templates/model_spec.rb +7 -0
  37. data/lib/generators/rspec/request/request_generator.rb +17 -0
  38. data/lib/generators/rspec/request/templates/request_spec.rb +10 -0
  39. data/lib/generators/rspec/scaffold/scaffold_generator.rb +136 -0
  40. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +129 -0
  41. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  42. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +160 -0
  43. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +27 -0
  44. data/lib/generators/rspec/scaffold/templates/index_spec.rb +26 -0
  45. data/lib/generators/rspec/scaffold/templates/new_spec.rb +22 -0
  46. data/lib/generators/rspec/scaffold/templates/request_spec.rb +153 -0
  47. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +46 -0
  48. data/lib/generators/rspec/scaffold/templates/show_spec.rb +21 -0
  49. data/lib/generators/rspec/system/system_generator.rb +24 -0
  50. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  51. data/lib/generators/rspec/view/templates/view_spec.rb +5 -0
  52. data/lib/generators/rspec/view/view_generator.rb +22 -0
  53. data/lib/generators/rspec.rb +56 -0
  54. data/lib/rspec/rails/active_record.rb +25 -0
  55. data/lib/rspec/rails/adapters.rb +196 -0
  56. data/lib/rspec/rails/configuration.rb +222 -0
  57. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  58. data/lib/rspec/rails/example/controller_example_group.rb +217 -0
  59. data/lib/rspec/rails/example/feature_example_group.rb +53 -0
  60. data/lib/rspec/rails/example/helper_example_group.rb +42 -0
  61. data/lib/rspec/rails/example/job_example_group.rb +23 -0
  62. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  63. data/lib/rspec/rails/example/mailer_example_group.rb +38 -0
  64. data/lib/rspec/rails/example/model_example_group.rb +11 -0
  65. data/lib/rspec/rails/example/rails_example_group.rb +25 -0
  66. data/lib/rspec/rails/example/request_example_group.rb +27 -0
  67. data/lib/rspec/rails/example/routing_example_group.rb +61 -0
  68. data/lib/rspec/rails/example/system_example_group.rb +172 -0
  69. data/lib/rspec/rails/example/view_example_group.rb +214 -0
  70. data/lib/rspec/rails/example.rb +13 -0
  71. data/lib/rspec/rails/extensions/active_record/proxy.rb +11 -0
  72. data/lib/rspec/rails/extensions.rb +1 -0
  73. data/lib/rspec/rails/feature_check.rb +51 -0
  74. data/lib/rspec/rails/file_fixture_support.rb +18 -0
  75. data/lib/rspec/rails/fixture_file_upload_support.rb +45 -0
  76. data/lib/rspec/rails/fixture_support.rb +89 -0
  77. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +173 -0
  78. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  79. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  80. data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
  81. data/lib/rspec/rails/matchers/active_job.rb +465 -0
  82. data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
  83. data/lib/rspec/rails/matchers/be_a_new.rb +83 -0
  84. data/lib/rspec/rails/matchers/be_new_record.rb +30 -0
  85. data/lib/rspec/rails/matchers/be_valid.rb +49 -0
  86. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +227 -0
  87. data/lib/rspec/rails/matchers/have_http_status.rb +385 -0
  88. data/lib/rspec/rails/matchers/have_rendered.rb +64 -0
  89. data/lib/rspec/rails/matchers/redirect_to.rb +38 -0
  90. data/lib/rspec/rails/matchers/relation_match_array.rb +3 -0
  91. data/lib/rspec/rails/matchers/routing_matchers.rb +125 -0
  92. data/lib/rspec/rails/matchers/send_email.rb +122 -0
  93. data/lib/rspec/rails/matchers.rb +36 -0
  94. data/lib/rspec/rails/tasks/rspec.rake +47 -0
  95. data/lib/rspec/rails/vendor/capybara.rb +32 -0
  96. data/lib/rspec/rails/version.rb +9 -0
  97. data/lib/rspec/rails/view_assigns.rb +27 -0
  98. data/lib/rspec/rails/view_path_builder.rb +29 -0
  99. data/lib/rspec/rails/view_rendering.rb +166 -0
  100. data/lib/rspec/rails/view_spec_methods.rb +56 -0
  101. data/lib/rspec/rails.rb +18 -0
  102. data/lib/rspec-rails.rb +80 -0
  103. data.tar.gz.sig +2 -0
  104. metadata +308 -261
  105. metadata.gz.sig +0 -0
  106. data/Contribute.rdoc +0 -4
  107. data/History.rdoc +0 -302
  108. data/License.txt +0 -33
  109. data/Manifest.txt +0 -165
  110. data/README.rdoc +0 -45
  111. data/Rakefile +0 -72
  112. data/TODO.txt +0 -17
  113. data/Upgrade.rdoc +0 -148
  114. data/generators/integration_spec/integration_spec_generator.rb +0 -10
  115. data/generators/integration_spec/templates/integration_spec.rb +0 -4
  116. data/generators/rspec/CHANGES +0 -1
  117. data/generators/rspec/rspec_generator.rb +0 -72
  118. data/generators/rspec/templates/previous_failures.txt +0 -0
  119. data/generators/rspec/templates/rcov.opts +0 -2
  120. data/generators/rspec/templates/rspec.rake +0 -144
  121. data/generators/rspec/templates/script/autospec +0 -6
  122. data/generators/rspec/templates/script/spec +0 -10
  123. data/generators/rspec/templates/spec.opts +0 -4
  124. data/generators/rspec/templates/spec_helper.rb +0 -54
  125. data/generators/rspec_controller/USAGE +0 -33
  126. data/generators/rspec_controller/rspec_controller_generator.rb +0 -47
  127. data/generators/rspec_controller/templates/controller_spec.rb +0 -25
  128. data/generators/rspec_controller/templates/helper_spec.rb +0 -11
  129. data/generators/rspec_controller/templates/view_spec.rb +0 -12
  130. data/generators/rspec_default_values.rb +0 -28
  131. data/generators/rspec_model/USAGE +0 -18
  132. data/generators/rspec_model/rspec_model_generator.rb +0 -35
  133. data/generators/rspec_model/templates/model_spec.rb +0 -13
  134. data/generators/rspec_scaffold/rspec_scaffold_generator.rb +0 -154
  135. data/generators/rspec_scaffold/templates/controller_spec.rb +0 -131
  136. data/generators/rspec_scaffold/templates/edit_erb_spec.rb +0 -25
  137. data/generators/rspec_scaffold/templates/helper_spec.rb +0 -11
  138. data/generators/rspec_scaffold/templates/index_erb_spec.rb +0 -27
  139. data/generators/rspec_scaffold/templates/new_erb_spec.rb +0 -25
  140. data/generators/rspec_scaffold/templates/routing_spec.rb +0 -33
  141. data/generators/rspec_scaffold/templates/show_erb_spec.rb +0 -22
  142. data/init.rb +0 -9
  143. data/lib/autotest/discover.rb +0 -5
  144. data/lib/autotest/rails_rspec.rb +0 -76
  145. data/lib/spec/rails/example/assigns_hash_proxy.rb +0 -39
  146. data/lib/spec/rails/example/controller_example_group.rb +0 -285
  147. data/lib/spec/rails/example/cookies_proxy.rb +0 -29
  148. data/lib/spec/rails/example/functional_example_group.rb +0 -106
  149. data/lib/spec/rails/example/helper_example_group.rb +0 -153
  150. data/lib/spec/rails/example/integration_example_group.rb +0 -16
  151. data/lib/spec/rails/example/model_example_group.rb +0 -15
  152. data/lib/spec/rails/example/render_observer.rb +0 -80
  153. data/lib/spec/rails/example/routing_example_group.rb +0 -13
  154. data/lib/spec/rails/example/routing_helpers.rb +0 -66
  155. data/lib/spec/rails/example/view_example_group.rb +0 -199
  156. data/lib/spec/rails/example.rb +0 -48
  157. data/lib/spec/rails/extensions/action_controller/rescue.rb +0 -42
  158. data/lib/spec/rails/extensions/action_controller/test_case.rb +0 -16
  159. data/lib/spec/rails/extensions/action_controller/test_response.rb +0 -21
  160. data/lib/spec/rails/extensions/action_view/base.rb +0 -33
  161. data/lib/spec/rails/extensions/active_record/base.rb +0 -45
  162. data/lib/spec/rails/extensions/active_support/test_case.rb +0 -7
  163. data/lib/spec/rails/extensions/spec/matchers/have.rb +0 -23
  164. data/lib/spec/rails/extensions/spec/runner/configuration.rb +0 -44
  165. data/lib/spec/rails/extensions.rb +0 -11
  166. data/lib/spec/rails/interop/testcase.rb +0 -14
  167. data/lib/spec/rails/matchers/ar_be_valid.rb +0 -27
  168. data/lib/spec/rails/matchers/assert_select.rb +0 -180
  169. data/lib/spec/rails/matchers/change.rb +0 -13
  170. data/lib/spec/rails/matchers/have_text.rb +0 -57
  171. data/lib/spec/rails/matchers/include_text.rb +0 -54
  172. data/lib/spec/rails/matchers/redirect_to.rb +0 -126
  173. data/lib/spec/rails/matchers/render_template.rb +0 -129
  174. data/lib/spec/rails/matchers/route_to.rb +0 -149
  175. data/lib/spec/rails/matchers.rb +0 -32
  176. data/lib/spec/rails/mocks.rb +0 -136
  177. data/lib/spec/rails/version.rb +0 -16
  178. data/lib/spec/rails.rb +0 -26
  179. data/spec/autotest/mappings_spec.rb +0 -86
  180. data/spec/rails_suite.rb +0 -7
  181. data/spec/resources/controllers/action_view_base_spec_controller.rb +0 -2
  182. data/spec/resources/controllers/application.rb +0 -9
  183. data/spec/resources/controllers/controller_spec_controller.rb +0 -127
  184. data/spec/resources/controllers/example.txt +0 -1
  185. data/spec/resources/controllers/redirect_spec_controller.rb +0 -70
  186. data/spec/resources/controllers/render_spec_controller.rb +0 -34
  187. data/spec/resources/controllers/rjs_spec_controller.rb +0 -58
  188. data/spec/resources/helpers/addition_helper.rb +0 -5
  189. data/spec/resources/helpers/explicit_helper.rb +0 -46
  190. data/spec/resources/helpers/more_explicit_helper.rb +0 -5
  191. data/spec/resources/helpers/plugin_application_helper.rb +0 -6
  192. data/spec/resources/helpers/view_spec_helper.rb +0 -13
  193. data/spec/resources/models/animal.rb +0 -4
  194. data/spec/resources/models/person.rb +0 -18
  195. data/spec/resources/models/thing.rb +0 -3
  196. data/spec/resources/views/controller_spec/_partial.html.erb +0 -0
  197. data/spec/resources/views/controller_spec/action_setting_flash_after_session_reset.html.erb +0 -1
  198. data/spec/resources/views/controller_spec/action_setting_flash_before_session_reset.html.erb +0 -1
  199. data/spec/resources/views/controller_spec/action_setting_the_assigns_hash.html.erb +0 -0
  200. data/spec/resources/views/controller_spec/action_with_errors_in_template.html.erb +0 -1
  201. data/spec/resources/views/controller_spec/action_with_template.html.erb +0 -1
  202. data/spec/resources/views/layouts/application.html.erb +0 -0
  203. data/spec/resources/views/layouts/simple.html.erb +0 -0
  204. data/spec/resources/views/objects/_object.html.erb +0 -1
  205. data/spec/resources/views/render_spec/_a_partial.html.erb +0 -0
  206. data/spec/resources/views/render_spec/action_with_alternate_layout.html.erb +0 -0
  207. data/spec/resources/views/render_spec/some_action.html.erb +0 -0
  208. data/spec/resources/views/render_spec/some_action.js.rjs +0 -1
  209. data/spec/resources/views/rjs_spec/_replacement_partial.html.erb +0 -1
  210. data/spec/resources/views/rjs_spec/hide_div.js.rjs +0 -1
  211. data/spec/resources/views/rjs_spec/hide_page_element.js.rjs +0 -1
  212. data/spec/resources/views/rjs_spec/insert_html.js.rjs +0 -1
  213. data/spec/resources/views/rjs_spec/replace.js.rjs +0 -1
  214. data/spec/resources/views/rjs_spec/replace_html.js.rjs +0 -1
  215. data/spec/resources/views/rjs_spec/replace_html_with_partial.js.rjs +0 -1
  216. data/spec/resources/views/rjs_spec/visual_effect.js.rjs +0 -1
  217. data/spec/resources/views/rjs_spec/visual_toggle_effect.js.rjs +0 -1
  218. data/spec/resources/views/tag_spec/no_tags.html.erb +0 -1
  219. data/spec/resources/views/tag_spec/single_div_with_no_attributes.html.erb +0 -1
  220. data/spec/resources/views/tag_spec/single_div_with_one_attribute.html.erb +0 -1
  221. data/spec/resources/views/view_spec/_partial.html.erb +0 -2
  222. data/spec/resources/views/view_spec/_partial_used_twice.html.erb +0 -0
  223. data/spec/resources/views/view_spec/_partial_with_local_variable.html.erb +0 -1
  224. data/spec/resources/views/view_spec/_partial_with_sub_partial.html.erb +0 -1
  225. data/spec/resources/views/view_spec/_spacer.html.erb +0 -1
  226. data/spec/resources/views/view_spec/accessor.html.erb +0 -6
  227. data/spec/resources/views/view_spec/block_helper.html.erb +0 -3
  228. data/spec/resources/views/view_spec/entry_form.html.erb +0 -2
  229. data/spec/resources/views/view_spec/explicit_helper.html.erb +0 -2
  230. data/spec/resources/views/view_spec/foo/show.html.erb +0 -1
  231. data/spec/resources/views/view_spec/implicit_helper.html.erb +0 -2
  232. data/spec/resources/views/view_spec/multiple_helpers.html.erb +0 -3
  233. data/spec/resources/views/view_spec/path_params.html.erb +0 -1
  234. data/spec/resources/views/view_spec/should_not_receive.html.erb +0 -3
  235. data/spec/resources/views/view_spec/template_with_partial.html.erb +0 -5
  236. data/spec/resources/views/view_spec/template_with_partial_using_collection.html.erb +0 -3
  237. data/spec/resources/views/view_spec/template_with_partial_with_array.html.erb +0 -1
  238. data/spec/resources/views/view_spec/view_helpers.html.erb +0 -1
  239. data/spec/spec/rails/example/assigns_hash_proxy_spec.rb +0 -109
  240. data/spec/spec/rails/example/configuration_spec.rb +0 -65
  241. data/spec/spec/rails/example/controller_example_group_spec.rb +0 -307
  242. data/spec/spec/rails/example/controller_isolation_spec.rb +0 -75
  243. data/spec/spec/rails/example/cookies_proxy_spec.rb +0 -87
  244. data/spec/spec/rails/example/error_handling_spec.rb +0 -90
  245. data/spec/spec/rails/example/example_group_factory_spec.rb +0 -112
  246. data/spec/spec/rails/example/helper_example_group_spec.rb +0 -233
  247. data/spec/spec/rails/example/model_example_group_spec.rb +0 -32
  248. data/spec/spec/rails/example/routing_example_group_spec.rb +0 -10
  249. data/spec/spec/rails/example/shared_routing_example_group_examples.rb +0 -237
  250. data/spec/spec/rails/example/test_unit_assertion_accessibility_spec.rb +0 -33
  251. data/spec/spec/rails/example/view_example_group_spec.rb +0 -346
  252. data/spec/spec/rails/extensions/action_view_base_spec.rb +0 -74
  253. data/spec/spec/rails/extensions/active_record_spec.rb +0 -14
  254. data/spec/spec/rails/interop/testcase_spec.rb +0 -70
  255. data/spec/spec/rails/matchers/ar_be_valid_spec.rb +0 -19
  256. data/spec/spec/rails/matchers/assert_select_spec.rb +0 -835
  257. data/spec/spec/rails/matchers/errors_on_spec.rb +0 -37
  258. data/spec/spec/rails/matchers/have_text_spec.rb +0 -69
  259. data/spec/spec/rails/matchers/include_text_spec.rb +0 -62
  260. data/spec/spec/rails/matchers/redirect_to_spec.rb +0 -253
  261. data/spec/spec/rails/matchers/render_template_spec.rb +0 -208
  262. data/spec/spec/rails/matchers/should_change_spec.rb +0 -15
  263. data/spec/spec/rails/mocks/ar_classes.rb +0 -10
  264. data/spec/spec/rails/mocks/mock_model_spec.rb +0 -109
  265. data/spec/spec/rails/mocks/stub_model_spec.rb +0 -80
  266. data/spec/spec/rails/sample_modified_fixture.rb +0 -8
  267. data/spec/spec/rails/sample_spec.rb +0 -8
  268. data/spec/spec/rails/spec_spec.rb +0 -11
  269. data/spec/spec_helper.rb +0 -78
@@ -0,0 +1,465 @@
1
+ require "active_job/base"
2
+ require "active_job/arguments"
3
+
4
+ module RSpec
5
+ module Rails
6
+ module Matchers
7
+ # Namespace for various implementations of ActiveJob features
8
+ #
9
+ # @api private
10
+ module ActiveJob
11
+ # rubocop: disable Metrics/ClassLength
12
+ # @private
13
+ class Base < RSpec::Rails::Matchers::BaseMatcher
14
+ def initialize
15
+ @args = []
16
+ @queue = nil
17
+ @at = nil
18
+ @block = proc { }
19
+ set_expected_number(:exactly, 1)
20
+ end
21
+
22
+ def with(*args, &block)
23
+ @args = args
24
+ @block = block if block.present?
25
+ self
26
+ end
27
+
28
+ def on_queue(queue)
29
+ @queue = queue.to_s
30
+ self
31
+ end
32
+
33
+ def at(time_or_date)
34
+ case time_or_date
35
+ when Time then @at = Time.at(time_or_date.to_f)
36
+ else
37
+ @at = time_or_date
38
+ end
39
+ self
40
+ end
41
+
42
+ def exactly(count)
43
+ set_expected_number(:exactly, count)
44
+ self
45
+ end
46
+
47
+ def at_least(count)
48
+ set_expected_number(:at_least, count)
49
+ self
50
+ end
51
+
52
+ def at_most(count)
53
+ set_expected_number(:at_most, count)
54
+ self
55
+ end
56
+
57
+ def times
58
+ self
59
+ end
60
+
61
+ def once
62
+ exactly(:once)
63
+ end
64
+
65
+ def twice
66
+ exactly(:twice)
67
+ end
68
+
69
+ def thrice
70
+ exactly(:thrice)
71
+ end
72
+
73
+ def failure_message
74
+ "expected to #{self.class::FAILURE_MESSAGE_EXPECTATION_ACTION} #{base_message}".tap do |msg|
75
+ if @unmatching_jobs.any?
76
+ msg << "\nQueued jobs:"
77
+ @unmatching_jobs.each do |job|
78
+ msg << "\n #{base_job_message(job)}"
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ def failure_message_when_negated
85
+ "expected not to #{self.class::FAILURE_MESSAGE_EXPECTATION_ACTION} #{base_message}"
86
+ end
87
+
88
+ def message_expectation_modifier
89
+ case @expectation_type
90
+ when :exactly then "exactly"
91
+ when :at_most then "at most"
92
+ when :at_least then "at least"
93
+ end
94
+ end
95
+
96
+ def supports_block_expectations?
97
+ true
98
+ end
99
+
100
+ private
101
+
102
+ def check(jobs)
103
+ @matching_jobs, @unmatching_jobs = jobs.partition do |job|
104
+ if job_match?(job) && arguments_match?(job) && queue_match?(job) && at_match?(job)
105
+ args = deserialize_arguments(job)
106
+ @block.call(*args)
107
+ true
108
+ else
109
+ false
110
+ end
111
+ end
112
+ @matching_jobs_count = @matching_jobs.size
113
+
114
+ case @expectation_type
115
+ when :exactly then @expected_number == @matching_jobs_count
116
+ when :at_most then @expected_number >= @matching_jobs_count
117
+ when :at_least then @expected_number <= @matching_jobs_count
118
+ end
119
+ end
120
+
121
+ def base_message
122
+ "#{message_expectation_modifier} #{@expected_number} jobs,".tap do |msg|
123
+ msg << " with #{@args}," if @args.any?
124
+ msg << " on queue #{@queue}," if @queue
125
+ msg << " at #{@at.inspect}," if @at
126
+ msg << " but #{self.class::MESSAGE_EXPECTATION_ACTION} #{@matching_jobs_count}"
127
+ end
128
+ end
129
+
130
+ def base_job_message(job)
131
+ msg_parts = []
132
+ msg_parts << "with #{deserialize_arguments(job)}" if job[:args].any?
133
+ msg_parts << "on queue #{job[:queue]}" if job[:queue]
134
+ msg_parts << "at #{Time.at(job[:at])}" if job[:at]
135
+
136
+ "#{job[:job].name} job".tap do |msg|
137
+ msg << " #{msg_parts.join(', ')}" if msg_parts.any?
138
+ end
139
+ end
140
+
141
+ def job_match?(job)
142
+ @job ? @job == job[:job] : true
143
+ end
144
+
145
+ def arguments_match?(job)
146
+ if @args.any?
147
+ args = serialize_and_deserialize_arguments(@args)
148
+ deserialized_args = deserialize_arguments(job)
149
+ RSpec::Mocks::ArgumentListMatcher.new(*args).args_match?(*deserialized_args)
150
+ else
151
+ true
152
+ end
153
+ end
154
+
155
+ def queue_match?(job)
156
+ return true unless @queue
157
+
158
+ @queue == job[:queue]
159
+ end
160
+
161
+ def at_match?(job)
162
+ return true unless @at
163
+ return job[:at].nil? if @at == :no_wait
164
+ return false unless job[:at]
165
+
166
+ scheduled_at = Time.at(job[:at])
167
+ values_match?(@at, scheduled_at) || check_for_inprecise_value(scheduled_at)
168
+ end
169
+
170
+ def check_for_inprecise_value(scheduled_at)
171
+ return unless Time === @at && values_match?(@at.change(usec: 0), scheduled_at)
172
+
173
+ RSpec.warn_with((<<-WARNING).gsub(/^\s+\|/, '').chomp)
174
+ |[WARNING] Your expected `at(...)` value does not match the job scheduled_at value
175
+ |unless microseconds are removed. This precision error often occurs when checking
176
+ |values against `Time.current` / `Time.now` which have usec precision, but Rails
177
+ |uses `n.seconds.from_now` internally which has a usec count of `0`.
178
+ |
179
+ |Use `change(usec: 0)` to correct these values. For example:
180
+ |
181
+ |`Time.current.change(usec: 0)`
182
+ |
183
+ |Note: RSpec cannot do this for you because jobs can be scheduled with usec
184
+ |precision and we do not know whether it is on purpose or not.
185
+ |
186
+ |
187
+ WARNING
188
+ false
189
+ end
190
+
191
+ def set_expected_number(relativity, count)
192
+ @expectation_type = relativity
193
+ @expected_number = case count
194
+ when :once then 1
195
+ when :twice then 2
196
+ when :thrice then 3
197
+ else Integer(count)
198
+ end
199
+ end
200
+
201
+ def serialize_and_deserialize_arguments(args)
202
+ serialized = ::ActiveJob::Arguments.serialize(args)
203
+ ::ActiveJob::Arguments.deserialize(serialized)
204
+ rescue ::ActiveJob::SerializationError
205
+ args
206
+ end
207
+
208
+ def deserialize_arguments(job)
209
+ ::ActiveJob::Arguments.deserialize(job[:args])
210
+ rescue ::ActiveJob::DeserializationError
211
+ job[:args]
212
+ end
213
+
214
+ def queue_adapter
215
+ ::ActiveJob::Base.queue_adapter
216
+ end
217
+ end
218
+ # rubocop: enable Metrics/ClassLength
219
+
220
+ # @private
221
+ class HaveEnqueuedJob < Base
222
+ FAILURE_MESSAGE_EXPECTATION_ACTION = 'enqueue'.freeze
223
+ MESSAGE_EXPECTATION_ACTION = 'enqueued'.freeze
224
+
225
+ def initialize(job)
226
+ super()
227
+ @job = job
228
+ end
229
+
230
+ def matches?(proc)
231
+ raise ArgumentError, "have_enqueued_job and enqueue_job only support block expectations" unless Proc === proc
232
+
233
+ original_enqueued_jobs = Set.new(queue_adapter.enqueued_jobs)
234
+ proc.call
235
+ enqueued_jobs = Set.new(queue_adapter.enqueued_jobs)
236
+
237
+ check(enqueued_jobs - original_enqueued_jobs)
238
+ end
239
+
240
+ def does_not_match?(proc)
241
+ set_expected_number(:at_least, 1)
242
+
243
+ !matches?(proc)
244
+ end
245
+ end
246
+
247
+ # @private
248
+ class HaveBeenEnqueued < Base
249
+ FAILURE_MESSAGE_EXPECTATION_ACTION = 'enqueue'.freeze
250
+ MESSAGE_EXPECTATION_ACTION = 'enqueued'.freeze
251
+
252
+ def matches?(job)
253
+ @job = job
254
+ check(queue_adapter.enqueued_jobs)
255
+ end
256
+
257
+ def does_not_match?(proc)
258
+ set_expected_number(:at_least, 1)
259
+
260
+ !matches?(proc)
261
+ end
262
+ end
263
+
264
+ # @private
265
+ class HavePerformedJob < Base
266
+ FAILURE_MESSAGE_EXPECTATION_ACTION = 'perform'.freeze
267
+ MESSAGE_EXPECTATION_ACTION = 'performed'.freeze
268
+
269
+ def initialize(job)
270
+ super()
271
+ @job = job
272
+ end
273
+
274
+ def matches?(proc)
275
+ raise ArgumentError, "have_performed_job only supports block expectations" unless Proc === proc
276
+
277
+ original_performed_jobs_count = queue_adapter.performed_jobs.count
278
+ proc.call
279
+ in_block_jobs = queue_adapter.performed_jobs.drop(original_performed_jobs_count)
280
+
281
+ check(in_block_jobs)
282
+ end
283
+ end
284
+
285
+ # @private
286
+ class HaveBeenPerformed < Base
287
+ FAILURE_MESSAGE_EXPECTATION_ACTION = 'perform'.freeze
288
+ MESSAGE_EXPECTATION_ACTION = 'performed'.freeze
289
+
290
+ def matches?(job)
291
+ @job = job
292
+ check(queue_adapter.performed_jobs)
293
+ end
294
+ end
295
+ end
296
+
297
+ # @api public
298
+ # Passes if a job has been enqueued inside block. May chain at_least, at_most or exactly to specify a number of times.
299
+ #
300
+ # @example
301
+ # expect {
302
+ # HeavyLiftingJob.perform_later
303
+ # }.to have_enqueued_job
304
+ #
305
+ # # Using alias
306
+ # expect {
307
+ # HeavyLiftingJob.perform_later
308
+ # }.to enqueue_job
309
+ #
310
+ # expect {
311
+ # HelloJob.perform_later
312
+ # HeavyLiftingJob.perform_later
313
+ # }.to have_enqueued_job(HelloJob).exactly(:once)
314
+ #
315
+ # expect {
316
+ # 3.times { HelloJob.perform_later }
317
+ # }.to have_enqueued_job(HelloJob).at_least(2).times
318
+ #
319
+ # expect {
320
+ # HelloJob.perform_later
321
+ # }.to have_enqueued_job(HelloJob).at_most(:twice)
322
+ #
323
+ # expect {
324
+ # HelloJob.perform_later
325
+ # HeavyLiftingJob.perform_later
326
+ # }.to have_enqueued_job(HelloJob).and have_enqueued_job(HeavyLiftingJob)
327
+ #
328
+ # expect {
329
+ # HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
330
+ # }.to have_enqueued_job.with(42).on_queue("low").at(Date.tomorrow.noon)
331
+ #
332
+ # expect {
333
+ # HelloJob.set(queue: "low").perform_later(42)
334
+ # }.to have_enqueued_job.with(42).on_queue("low").at(:no_wait)
335
+ #
336
+ # expect {
337
+ # HelloJob.perform_later('rspec_rails', 'rails', 42)
338
+ # }.to have_enqueued_job.with { |from, to, times|
339
+ # # Perform more complex argument matching using dynamic arguments
340
+ # expect(from).to include "_#{to}"
341
+ # }
342
+ def have_enqueued_job(job = nil)
343
+ check_active_job_adapter
344
+ ActiveJob::HaveEnqueuedJob.new(job)
345
+ end
346
+ alias_method :enqueue_job, :have_enqueued_job
347
+
348
+ # @api public
349
+ # Passes if a job has been enqueued. May chain at_least, at_most or exactly to specify a number of times.
350
+ #
351
+ # @example
352
+ # before { ActiveJob::Base.queue_adapter.enqueued_jobs.clear }
353
+ #
354
+ # HeavyLiftingJob.perform_later
355
+ # expect(HeavyLiftingJob).to have_been_enqueued
356
+ #
357
+ # HelloJob.perform_later
358
+ # HeavyLiftingJob.perform_later
359
+ # expect(HeavyLiftingJob).to have_been_enqueued.exactly(:once)
360
+ #
361
+ # 3.times { HelloJob.perform_later }
362
+ # expect(HelloJob).to have_been_enqueued.at_least(2).times
363
+ #
364
+ # HelloJob.perform_later
365
+ # expect(HelloJob).to enqueue_job(HelloJob).at_most(:twice)
366
+ #
367
+ # HelloJob.perform_later
368
+ # HeavyLiftingJob.perform_later
369
+ # expect(HelloJob).to have_been_enqueued
370
+ # expect(HeavyLiftingJob).to have_been_enqueued
371
+ #
372
+ # HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
373
+ # expect(HelloJob).to have_been_enqueued.with(42).on_queue("low").at(Date.tomorrow.noon)
374
+ #
375
+ # HelloJob.set(queue: "low").perform_later(42)
376
+ # expect(HelloJob).to have_been_enqueued.with(42).on_queue("low").at(:no_wait)
377
+ def have_been_enqueued
378
+ check_active_job_adapter
379
+ ActiveJob::HaveBeenEnqueued.new
380
+ end
381
+
382
+ # @api public
383
+ # Passes if a job has been performed inside block. May chain at_least, at_most or exactly to specify a number of times.
384
+ #
385
+ # @example
386
+ # expect {
387
+ # perform_jobs { HeavyLiftingJob.perform_later }
388
+ # }.to have_performed_job
389
+ #
390
+ # expect {
391
+ # perform_jobs {
392
+ # HelloJob.perform_later
393
+ # HeavyLiftingJob.perform_later
394
+ # }
395
+ # }.to have_performed_job(HelloJob).exactly(:once)
396
+ #
397
+ # expect {
398
+ # perform_jobs { 3.times { HelloJob.perform_later } }
399
+ # }.to have_performed_job(HelloJob).at_least(2).times
400
+ #
401
+ # expect {
402
+ # perform_jobs { HelloJob.perform_later }
403
+ # }.to have_performed_job(HelloJob).at_most(:twice)
404
+ #
405
+ # expect {
406
+ # perform_jobs {
407
+ # HelloJob.perform_later
408
+ # HeavyLiftingJob.perform_later
409
+ # }
410
+ # }.to have_performed_job(HelloJob).and have_performed_job(HeavyLiftingJob)
411
+ #
412
+ # expect {
413
+ # perform_jobs {
414
+ # HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
415
+ # }
416
+ # }.to have_performed_job.with(42).on_queue("low").at(Date.tomorrow.noon)
417
+ def have_performed_job(job = nil)
418
+ check_active_job_adapter
419
+ ActiveJob::HavePerformedJob.new(job)
420
+ end
421
+ alias_method :perform_job, :have_performed_job
422
+
423
+ # @api public
424
+ # Passes if a job has been performed. May chain at_least, at_most or exactly to specify a number of times.
425
+ #
426
+ # @example
427
+ # before do
428
+ # ActiveJob::Base.queue_adapter.performed_jobs.clear
429
+ # ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
430
+ # ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = true
431
+ # end
432
+ #
433
+ # HeavyLiftingJob.perform_later
434
+ # expect(HeavyLiftingJob).to have_been_performed
435
+ #
436
+ # HelloJob.perform_later
437
+ # HeavyLiftingJob.perform_later
438
+ # expect(HeavyLiftingJob).to have_been_performed.exactly(:once)
439
+ #
440
+ # 3.times { HelloJob.perform_later }
441
+ # expect(HelloJob).to have_been_performed.at_least(2).times
442
+ #
443
+ # HelloJob.perform_later
444
+ # HeavyLiftingJob.perform_later
445
+ # expect(HelloJob).to have_been_performed
446
+ # expect(HeavyLiftingJob).to have_been_performed
447
+ #
448
+ # HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
449
+ # expect(HelloJob).to have_been_performed.with(42).on_queue("low").at(Date.tomorrow.noon)
450
+ def have_been_performed
451
+ check_active_job_adapter
452
+ ActiveJob::HaveBeenPerformed.new
453
+ end
454
+
455
+ private
456
+
457
+ # @private
458
+ def check_active_job_adapter
459
+ return if ::ActiveJob::QueueAdapters::TestAdapter === ::ActiveJob::Base.queue_adapter
460
+
461
+ raise StandardError, "To use ActiveJob matchers set `ActiveJob::Base.queue_adapter = :test`"
462
+ end
463
+ end
464
+ end
465
+ end
@@ -0,0 +1,179 @@
1
+ module RSpec
2
+ module Rails
3
+ module Matchers
4
+ # @api private
5
+ #
6
+ # Base class to build matchers. Should not be instantiated directly.
7
+ class BaseMatcher
8
+ include RSpec::Matchers::Composable
9
+
10
+ # @api private
11
+ # Used to detect when no arg is passed to `initialize`.
12
+ # `nil` cannot be used because it's a valid value to pass.
13
+ UNDEFINED = Object.new.freeze
14
+
15
+ # @private
16
+ attr_reader :actual, :expected, :rescued_exception
17
+
18
+ # @private
19
+ attr_writer :matcher_name
20
+
21
+ def initialize(expected = UNDEFINED)
22
+ @expected = expected unless UNDEFINED.equal?(expected)
23
+ end
24
+
25
+ # @api private
26
+ # Indicates if the match is successful. Delegates to `match`, which
27
+ # should be defined on a subclass. Takes care of consistently
28
+ # initializing the `actual` attribute.
29
+ def matches?(actual)
30
+ @actual = actual
31
+ match(expected, actual)
32
+ end
33
+
34
+ # @api private
35
+ # Used to wrap a block of code that will indicate failure by
36
+ # raising one of the named exceptions.
37
+ #
38
+ # This is used by rspec-rails for some of its matchers that
39
+ # wrap rails' assertions.
40
+ def match_unless_raises(*exceptions)
41
+ exceptions.unshift Exception if exceptions.empty?
42
+ begin
43
+ yield
44
+ true
45
+ rescue *exceptions => @rescued_exception
46
+ false
47
+ end
48
+ end
49
+
50
+ # @api private
51
+ # Generates a description using {RSpec::Matchers::EnglishPhrasing}.
52
+ # @return [String]
53
+ def description
54
+ desc = RSpec::Matchers::EnglishPhrasing.split_words(self.class.matcher_name)
55
+ desc << RSpec::Matchers::EnglishPhrasing.list(@expected) if defined?(@expected)
56
+ desc
57
+ end
58
+
59
+ # @api private
60
+ # Matchers are not diffable by default. Override this to make your
61
+ # subclass diffable.
62
+ def diffable?
63
+ false
64
+ end
65
+
66
+ # @api private
67
+ # Most matchers are value matchers (i.e. meant to work with `expect(value)`)
68
+ # rather than block matchers (i.e. meant to work with `expect { }`), so
69
+ # this defaults to false. Block matchers must override this to return true.
70
+ def supports_block_expectations?
71
+ false
72
+ end
73
+
74
+ # @api private
75
+ def expects_call_stack_jump?
76
+ false
77
+ end
78
+
79
+ # @private
80
+ def expected_formatted
81
+ RSpec::Support::ObjectFormatter.format(@expected)
82
+ end
83
+
84
+ # @private
85
+ def actual_formatted
86
+ RSpec::Support::ObjectFormatter.format(@actual)
87
+ end
88
+
89
+ # @private
90
+ def self.matcher_name
91
+ @matcher_name ||= underscore(name.split('::').last)
92
+ end
93
+
94
+ # @private
95
+ def matcher_name
96
+ if defined?(@matcher_name)
97
+ @matcher_name
98
+ else
99
+ self.class.matcher_name
100
+ end
101
+ end
102
+
103
+ # @private
104
+ # Borrowed from ActiveSupport.
105
+ def self.underscore(camel_cased_word)
106
+ word = camel_cased_word.to_s.dup
107
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
108
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
109
+ word.tr!('-', '_')
110
+ word.downcase!
111
+ word
112
+ end
113
+ private_class_method :underscore
114
+
115
+ private
116
+
117
+ def assert_ivars(*expected_ivars)
118
+ return unless (expected_ivars - present_ivars).any?
119
+
120
+ ivar_list = RSpec::Matchers::EnglishPhrasing.list(expected_ivars)
121
+ raise "#{self.class.name} needs to supply#{ivar_list}"
122
+ end
123
+
124
+ alias present_ivars instance_variables
125
+
126
+ # @private
127
+ module HashFormatting
128
+ # `{ :a => 5, :b => 2 }.inspect` produces:
129
+ #
130
+ # {:a=>5, :b=>2}
131
+ #
132
+ # ...but it looks much better as:
133
+ #
134
+ # {:a => 5, :b => 2}
135
+ #
136
+ # This is idempotent and safe to run on a string multiple times.
137
+ def improve_hash_formatting(inspect_string)
138
+ inspect_string.gsub(/(\S)=>(\S)/, '\1 => \2')
139
+ end
140
+ module_function :improve_hash_formatting
141
+ end
142
+
143
+ include HashFormatting
144
+
145
+ # @api private
146
+ # Provides default implementations of failure messages, based on the `description`.
147
+ module DefaultFailureMessages
148
+ # @api private
149
+ # Provides a good generic failure message. Based on `description`.
150
+ # When subclassing, if you are not satisfied with this failure message
151
+ # you often only need to override `description`.
152
+ # @return [String]
153
+ def failure_message
154
+ "expected #{description_of @actual} to #{description}".dup
155
+ end
156
+
157
+ # @api private
158
+ # Provides a good generic negative failure message. Based on `description`.
159
+ # When subclassing, if you are not satisfied with this failure message
160
+ # you often only need to override `description`.
161
+ # @return [String]
162
+ def failure_message_when_negated
163
+ "expected #{description_of @actual} not to #{description}".dup
164
+ end
165
+
166
+ # @private
167
+ def self.has_default_failure_messages?(matcher)
168
+ matcher.method(:failure_message).owner == self &&
169
+ matcher.method(:failure_message_when_negated).owner == self
170
+ rescue NameError
171
+ false
172
+ end
173
+ end
174
+
175
+ include DefaultFailureMessages
176
+ end
177
+ end
178
+ end
179
+ end