rspec-rails 2.14.2 → 4.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/.document +1 -1
  4. data/.yardopts +4 -2
  5. data/Capybara.md +6 -57
  6. data/Changelog.md +715 -37
  7. data/{License.txt → LICENSE.md} +5 -2
  8. data/README.md +287 -362
  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 +23 -5
  12. data/lib/generators/rspec/controller/templates/controller_spec.rb +5 -5
  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 +2 -2
  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 +6 -0
  21. data/lib/generators/rspec/helper/helper_generator.rb +2 -1
  22. data/lib/generators/rspec/helper/templates/helper_spec.rb +2 -2
  23. data/lib/generators/rspec/install/install_generator.rb +44 -5
  24. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +79 -0
  25. data/lib/generators/rspec/integration/integration_generator.rb +8 -13
  26. data/lib/generators/rspec/integration/templates/request_spec.rb +4 -9
  27. data/lib/generators/rspec/job/job_generator.rb +12 -0
  28. data/lib/generators/rspec/{observer/templates/observer_spec.rb → job/templates/job_spec.rb.erb} +2 -2
  29. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  30. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  31. data/lib/generators/rspec/mailer/mailer_generator.rb +9 -1
  32. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +7 -7
  33. data/lib/generators/rspec/mailer/templates/preview.rb +13 -0
  34. data/lib/generators/rspec/model/model_generator.rb +21 -6
  35. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  36. data/lib/generators/rspec/model/templates/model_spec.rb +2 -2
  37. data/lib/generators/rspec/request/request_generator.rb +10 -0
  38. data/lib/generators/rspec/scaffold/scaffold_generator.rb +88 -148
  39. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +129 -0
  40. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  41. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +105 -80
  42. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +10 -14
  43. data/lib/generators/rspec/scaffold/templates/index_spec.rb +5 -12
  44. data/lib/generators/rspec/scaffold/templates/new_spec.rb +11 -15
  45. data/lib/generators/rspec/scaffold/templates/request_spec.rb +137 -0
  46. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +19 -12
  47. data/lib/generators/rspec/scaffold/templates/show_spec.rb +5 -12
  48. data/lib/generators/rspec/system/system_generator.rb +26 -0
  49. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  50. data/lib/generators/rspec/view/templates/view_spec.rb +2 -2
  51. data/lib/generators/rspec/view/view_generator.rb +3 -2
  52. data/lib/generators/rspec.rb +18 -10
  53. data/lib/rspec/rails/active_record.rb +25 -0
  54. data/lib/rspec/rails/adapters.rb +49 -47
  55. data/lib/rspec/rails/configuration.rb +194 -0
  56. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  57. data/lib/rspec/rails/example/controller_example_group.rb +188 -138
  58. data/lib/rspec/rails/example/feature_example_group.rb +43 -20
  59. data/lib/rspec/rails/example/helper_example_group.rb +28 -26
  60. data/lib/rspec/rails/example/job_example_group.rb +23 -0
  61. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  62. data/lib/rspec/rails/example/mailer_example_group.rb +30 -14
  63. data/lib/rspec/rails/example/model_example_group.rb +8 -7
  64. data/lib/rspec/rails/example/rails_example_group.rb +4 -2
  65. data/lib/rspec/rails/example/request_example_group.rb +23 -16
  66. data/lib/rspec/rails/example/routing_example_group.rb +49 -40
  67. data/lib/rspec/rails/example/system_example_group.rb +125 -0
  68. data/lib/rspec/rails/example/view_example_group.rb +178 -134
  69. data/lib/rspec/rails/example.rb +4 -33
  70. data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -11
  71. data/lib/rspec/rails/extensions.rb +0 -1
  72. data/lib/rspec/rails/feature_check.rb +47 -0
  73. data/lib/rspec/rails/file_fixture_support.rb +17 -0
  74. data/lib/rspec/rails/fixture_file_upload_support.rb +35 -0
  75. data/lib/rspec/rails/fixture_support.rb +54 -23
  76. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
  77. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  78. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  79. data/lib/rspec/rails/matchers/action_mailbox.rb +64 -0
  80. data/lib/rspec/rails/matchers/active_job.rb +465 -0
  81. data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
  82. data/lib/rspec/rails/matchers/be_a_new.rb +69 -62
  83. data/lib/rspec/rails/matchers/be_new_record.rb +24 -21
  84. data/lib/rspec/rails/matchers/be_valid.rb +42 -33
  85. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +198 -0
  86. data/lib/rspec/rails/matchers/have_http_status.rb +385 -0
  87. data/lib/rspec/rails/matchers/have_rendered.rb +55 -31
  88. data/lib/rspec/rails/matchers/redirect_to.rb +30 -29
  89. data/lib/rspec/rails/matchers/relation_match_array.rb +2 -2
  90. data/lib/rspec/rails/matchers/routing_matchers.rb +107 -93
  91. data/lib/rspec/rails/matchers.rb +22 -14
  92. data/lib/rspec/rails/tasks/rspec.rake +8 -18
  93. data/lib/rspec/rails/vendor/capybara.rb +12 -11
  94. data/lib/rspec/rails/version.rb +3 -1
  95. data/lib/rspec/rails/view_assigns.rb +18 -18
  96. data/lib/rspec/rails/view_path_builder.rb +29 -0
  97. data/lib/rspec/rails/view_rendering.rb +100 -63
  98. data/lib/rspec/rails/view_spec_methods.rb +56 -0
  99. data/lib/rspec/rails.rb +10 -10
  100. data/lib/rspec-rails.rb +67 -3
  101. data.tar.gz.sig +0 -0
  102. metadata +121 -94
  103. metadata.gz.sig +0 -0
  104. data/lib/autotest/rails_rspec2.rb +0 -85
  105. data/lib/generators/rspec/install/templates/.rspec +0 -1
  106. data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +0 -49
  107. data/lib/generators/rspec/observer/observer_generator.rb +0 -12
  108. data/lib/rspec/rails/extensions/active_record/base.rb +0 -58
  109. data/lib/rspec/rails/matchers/have_extension.rb +0 -36
  110. data/lib/rspec/rails/mocks.rb +0 -274
  111. data/lib/rspec/rails/module_inclusion.rb +0 -19
  112. data/lib/rspec/rails/vendor/webrat.rb +0 -33
@@ -6,37 +6,8 @@ require 'rspec/rails/example/view_example_group'
6
6
  require 'rspec/rails/example/mailer_example_group'
7
7
  require 'rspec/rails/example/routing_example_group'
8
8
  require 'rspec/rails/example/model_example_group'
9
+ require 'rspec/rails/example/job_example_group'
9
10
  require 'rspec/rails/example/feature_example_group'
10
-
11
- RSpec::configure do |c|
12
- def c.escaped_path(*parts)
13
- Regexp.compile(parts.join('[\\\/]') + '[\\\/]')
14
- end
15
-
16
- c.include RSpec::Rails::ControllerExampleGroup, :type => :controller, :example_group => {
17
- :file_path => c.escaped_path(%w[spec controllers])
18
- }
19
- c.include RSpec::Rails::HelperExampleGroup, :type => :helper, :example_group => {
20
- :file_path => c.escaped_path(%w[spec helpers])
21
- }
22
- if defined?(RSpec::Rails::MailerExampleGroup)
23
- c.include RSpec::Rails::MailerExampleGroup, :type => :mailer, :example_group => {
24
- :file_path => c.escaped_path(%w[spec mailers])
25
- }
26
- end
27
- c.include RSpec::Rails::ModelExampleGroup, :type => :model, :example_group => {
28
- :file_path => c.escaped_path(%w[spec models])
29
- }
30
- c.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
31
- :file_path => c.escaped_path(%w[spec (requests|integration|api)])
32
- }
33
- c.include RSpec::Rails::RoutingExampleGroup, :type => :routing, :example_group => {
34
- :file_path => c.escaped_path(%w[spec routing])
35
- }
36
- c.include RSpec::Rails::ViewExampleGroup, :type => :view, :example_group => {
37
- :file_path => c.escaped_path(%w[spec views])
38
- }
39
- c.include RSpec::Rails::FeatureExampleGroup, :type => :feature, :example_group => {
40
- :file_path => c.escaped_path(%w[spec features])
41
- }
42
- end
11
+ require 'rspec/rails/example/system_example_group'
12
+ require 'rspec/rails/example/channel_example_group'
13
+ require 'rspec/rails/example/mailbox_example_group'
@@ -1,17 +1,11 @@
1
1
  RSpec.configure do |rspec|
2
2
  # Delay this in order to give users a chance to configure `expect_with`...
3
3
  rspec.before(:suite) do
4
- if defined?(RSpec::Matchers) && RSpec::Matchers.configuration.syntax.include?(:should) && defined?(ActiveRecord::Associations)
5
- # In Rails 3.0, it was AssociationProxy.
6
- # In 3.1+, it's CollectionProxy.
7
- const_name = [:CollectionProxy, :AssociationProxy].find do |const|
8
- ActiveRecord::Associations.const_defined?(const)
9
- end
10
-
11
- proxy_class = ActiveRecord::Associations.const_get(const_name)
12
-
13
- RSpec::Matchers.configuration.add_should_and_should_not_to proxy_class
4
+ if defined?(RSpec::Matchers) &&
5
+ RSpec::Matchers.configuration.respond_to?(:syntax) && # RSpec 4 dropped support for monkey-patching `should` syntax
6
+ RSpec::Matchers.configuration.syntax.include?(:should) &&
7
+ defined?(ActiveRecord::Associations)
8
+ RSpec::Matchers.configuration.add_should_and_should_not_to ActiveRecord::Associations::CollectionProxy
14
9
  end
15
10
  end
16
11
  end
17
-
@@ -1,2 +1 @@
1
- require 'rspec/rails/extensions/active_record/base'
2
1
  require 'rspec/rails/extensions/active_record/proxy'
@@ -0,0 +1,47 @@
1
+ module RSpec
2
+ module Rails
3
+ # @private
4
+ module FeatureCheck
5
+ module_function
6
+ def has_active_job?
7
+ defined?(::ActiveJob)
8
+ end
9
+
10
+ def has_active_record?
11
+ defined?(::ActiveRecord)
12
+ end
13
+
14
+ def has_active_record_migration?
15
+ has_active_record? && defined?(::ActiveRecord::Migration)
16
+ end
17
+
18
+ def has_action_mailer?
19
+ defined?(::ActionMailer)
20
+ end
21
+
22
+ def has_action_mailer_preview?
23
+ has_action_mailer? && defined?(::ActionMailer::Preview)
24
+ end
25
+
26
+ def has_action_cable_testing?
27
+ defined?(::ActionCable) && ActionCable::VERSION::MAJOR >= 6
28
+ end
29
+
30
+ def has_action_mailer_parameterized?
31
+ has_action_mailer? && defined?(::ActionMailer::Parameterized)
32
+ end
33
+
34
+ def has_action_mailer_unified_delivery?
35
+ has_action_mailer? && defined?(::ActionMailer::MailDeliveryJob)
36
+ end
37
+
38
+ def has_action_mailbox?
39
+ defined?(::ActionMailbox)
40
+ end
41
+
42
+ def type_metatag(type)
43
+ "type: :#{type}"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ if ::Rails::VERSION::STRING > '5'
2
+ require 'active_support/testing/file_fixtures'
3
+
4
+ module RSpec
5
+ module Rails
6
+ # @private
7
+ module FileFixtureSupport
8
+ extend ActiveSupport::Concern
9
+ include ActiveSupport::Testing::FileFixtures
10
+
11
+ included do
12
+ self.file_fixture_path = RSpec.configuration.file_fixture_path
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,35 @@
1
+ module RSpec
2
+ module Rails
3
+ # @private
4
+ module FixtureFileUploadSupport
5
+ delegate :fixture_file_upload, to: :rails_fixture_file_wrapper
6
+
7
+ private
8
+
9
+ def rails_fixture_file_wrapper
10
+ RailsFixtureFileWrapper.fixture_path = nil
11
+ resolved_fixture_path =
12
+ if respond_to?(:fixture_path) && !fixture_path.nil?
13
+ fixture_path.to_s
14
+ else
15
+ (RSpec.configuration.fixture_path || '').to_s
16
+ end
17
+ RailsFixtureFileWrapper.fixture_path = File.join(resolved_fixture_path, '') unless resolved_fixture_path.strip.empty?
18
+ RailsFixtureFileWrapper.instance
19
+ end
20
+
21
+ class RailsFixtureFileWrapper
22
+ include ActionDispatch::TestProcess if defined?(ActionDispatch::TestProcess)
23
+
24
+ class << self
25
+ attr_accessor :fixture_path
26
+
27
+ # Get instance of wrapper
28
+ def instance
29
+ @instance ||= new
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,38 +1,69 @@
1
1
  module RSpec
2
2
  module Rails
3
- if defined?(ActiveRecord::TestFixtures)
4
- module FixtureSupport
3
+ # @private
4
+ module FixtureSupport
5
+ if defined?(ActiveRecord::TestFixtures)
5
6
  extend ActiveSupport::Concern
6
7
  include RSpec::Rails::SetupAndTeardownAdapter
7
- include RSpec::Rails::MinitestLifecycleAdapter if ::ActiveRecord::VERSION::STRING > '4'
8
+ include RSpec::Rails::MinitestLifecycleAdapter
8
9
  include RSpec::Rails::MinitestAssertionAdapter
9
10
  include ActiveRecord::TestFixtures
10
11
 
12
+ # @private prevent ActiveSupport::TestFixtures to start a DB transaction.
13
+ # Monkey patched to avoid collisions with 'let(:name)' in Rails 6.1 and after
14
+ # and let(:method_name) before Rails 6.1.
15
+ if ::Rails::VERSION::STRING > '5'
16
+ def run_in_transaction?
17
+ use_transactional_tests && !self.class.uses_transaction?(self)
18
+ end
19
+ else
20
+ def run_in_transaction?
21
+ use_transactional_fixtures && !self.class.uses_transaction?(self)
22
+ end
23
+ end
24
+
11
25
  included do
12
- # TODO (DC 2011-06-25) this is necessary because fixture_file_upload
13
- # accesses fixture_path directly on ActiveSupport::TestCase. This is
14
- # fixed in rails by https://github.com/rails/rails/pull/1861, which
15
- # should be part of the 3.1 release, at which point we can include
16
- # these lines for rails < 3.1.
17
- ActiveSupport::TestCase.class_eval do
18
- include ActiveRecord::TestFixtures
26
+ if RSpec.configuration.use_active_record?
27
+ include Fixtures
28
+
19
29
  self.fixture_path = RSpec.configuration.fixture_path
20
- end
21
- # /TODO
30
+ if ::Rails::VERSION::STRING > '5'
31
+ self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
32
+ else
33
+ self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
34
+ end
35
+ self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
22
36
 
23
- self.fixture_path = RSpec.configuration.fixture_path
24
- self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
25
- self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
26
- fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
37
+ fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
38
+ end
27
39
  end
28
- end
29
40
 
30
- RSpec.configure do |c|
31
- c.include RSpec::Rails::FixtureSupport
32
- c.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
33
- c.add_setting :use_instantiated_fixtures
34
- c.add_setting :global_fixtures
35
- c.add_setting :fixture_path
41
+ module Fixtures
42
+ extend ActiveSupport::Concern
43
+
44
+ class_methods do
45
+ def fixtures(*args)
46
+ orig_methods = private_instance_methods
47
+ super.tap do
48
+ new_methods = private_instance_methods - orig_methods
49
+ new_methods.each do |method_name|
50
+ proxy_method_warning_if_called_in_before_context_scope(method_name)
51
+ end
52
+ end
53
+ end
54
+
55
+ def proxy_method_warning_if_called_in_before_context_scope(method_name)
56
+ orig_implementation = instance_method(method_name)
57
+ define_method(method_name) do |*args, &blk|
58
+ if inspect.include?("before(:context)")
59
+ RSpec.warn_with("Calling fixture method in before :context ")
60
+ else
61
+ orig_implementation.bind(self).call(*args, &blk)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
36
67
  end
37
68
  end
38
69
  end
@@ -0,0 +1,170 @@
1
+ module RSpec
2
+ module Rails
3
+ module Matchers
4
+ module ActionCable
5
+ # rubocop: disable Metrics/ClassLength
6
+ # @private
7
+ class HaveBroadcastedTo < RSpec::Matchers::BuiltIn::BaseMatcher
8
+ def initialize(target, channel:)
9
+ @target = target
10
+ @channel = channel
11
+ @block = proc { }
12
+ @data = nil
13
+ set_expected_number(:exactly, 1)
14
+ end
15
+
16
+ def with(data = nil, &block)
17
+ @data = data
18
+ @data = @data.with_indifferent_access if @data.is_a?(Hash)
19
+ @block = block if block
20
+ self
21
+ end
22
+
23
+ def exactly(count)
24
+ set_expected_number(:exactly, count)
25
+ self
26
+ end
27
+
28
+ def at_least(count)
29
+ set_expected_number(:at_least, count)
30
+ self
31
+ end
32
+
33
+ def at_most(count)
34
+ set_expected_number(:at_most, count)
35
+ self
36
+ end
37
+
38
+ def times
39
+ self
40
+ end
41
+
42
+ def once
43
+ exactly(:once)
44
+ end
45
+
46
+ def twice
47
+ exactly(:twice)
48
+ end
49
+
50
+ def thrice
51
+ exactly(:thrice)
52
+ end
53
+
54
+ def failure_message
55
+ "expected to broadcast #{base_message}".tap do |msg|
56
+ if @unmatching_msgs.any?
57
+ msg << "\nBroadcasted messages to #{stream}:"
58
+ @unmatching_msgs.each do |data|
59
+ msg << "\n #{data}"
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ def failure_message_when_negated
66
+ "expected not to broadcast #{base_message}"
67
+ end
68
+
69
+ def message_expectation_modifier
70
+ case @expectation_type
71
+ when :exactly then "exactly"
72
+ when :at_most then "at most"
73
+ when :at_least then "at least"
74
+ end
75
+ end
76
+
77
+ def supports_block_expectations?
78
+ true
79
+ end
80
+
81
+ def matches?(proc)
82
+ raise ArgumentError, "have_broadcasted_to and broadcast_to only support block expectations" unless Proc === proc
83
+
84
+ original_sent_messages_count = pubsub_adapter.broadcasts(stream).size
85
+ proc.call
86
+ in_block_messages = pubsub_adapter.broadcasts(stream).drop(original_sent_messages_count)
87
+
88
+ check(in_block_messages)
89
+ end
90
+
91
+ def from_channel(channel)
92
+ @channel = channel
93
+ self
94
+ end
95
+
96
+ private
97
+
98
+ def stream
99
+ @stream ||= if @target.is_a?(String)
100
+ @target
101
+ else
102
+ check_channel_presence
103
+ @channel.broadcasting_for(@target)
104
+ end
105
+ end
106
+
107
+ def check(messages)
108
+ @matching_msgs, @unmatching_msgs = messages.partition do |msg|
109
+ decoded = ActiveSupport::JSON.decode(msg)
110
+ decoded = decoded.with_indifferent_access if decoded.is_a?(Hash)
111
+
112
+ if @data.nil? || @data === decoded
113
+ @block.call(decoded)
114
+ true
115
+ else
116
+ false
117
+ end
118
+ end
119
+
120
+ @matching_msgs_count = @matching_msgs.size
121
+
122
+ case @expectation_type
123
+ when :exactly then @expected_number == @matching_msgs_count
124
+ when :at_most then @expected_number >= @matching_msgs_count
125
+ when :at_least then @expected_number <= @matching_msgs_count
126
+ end
127
+ end
128
+
129
+ def set_expected_number(relativity, count)
130
+ @expectation_type = relativity
131
+ @expected_number =
132
+ case count
133
+ when :once then 1
134
+ when :twice then 2
135
+ when :thrice then 3
136
+ else Integer(count)
137
+ end
138
+ end
139
+
140
+ def base_message
141
+ "#{message_expectation_modifier} #{@expected_number} messages to #{stream}".tap do |msg|
142
+ msg << " with #{data_description(@data)}" unless @data.nil?
143
+ msg << ", but broadcast #{@matching_msgs_count}"
144
+ end
145
+ end
146
+
147
+ def data_description(data)
148
+ if data.is_a?(RSpec::Matchers::Composable)
149
+ data.description
150
+ else
151
+ data
152
+ end
153
+ end
154
+
155
+ def pubsub_adapter
156
+ ::ActionCable.server.pubsub
157
+ end
158
+
159
+ def check_channel_presence
160
+ return if @channel.present? && @channel.respond_to?(:channel_name)
161
+
162
+ error_msg = "Broadcasting channel can't be infered. Please, specify it with `from_channel`"
163
+ raise ArgumentError, error_msg
164
+ end
165
+ end
166
+ # rubocop: enable Metrics/ClassLength
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,58 @@
1
+ module RSpec
2
+ module Rails
3
+ module Matchers
4
+ module ActionCable
5
+ # @api private
6
+ # Provides the implementation for `have_stream`, `have_stream_for`, and `have_stream_from`.
7
+ # Not intended to be instantiated directly.
8
+ class HaveStream < RSpec::Matchers::BuiltIn::BaseMatcher
9
+ # @api private
10
+ # @return [String]
11
+ def failure_message
12
+ "expected to have #{base_message}"
13
+ end
14
+
15
+ # @api private
16
+ # @return [String]
17
+ def failure_message_when_negated
18
+ "expected not to have #{base_message}"
19
+ end
20
+
21
+ # @api private
22
+ # @return [Boolean]
23
+ def matches?(subscription)
24
+ raise(ArgumentError, "have_streams is used for negated expectations only") if no_expected?
25
+
26
+ match(subscription)
27
+ end
28
+
29
+ # @api private
30
+ # @return [Boolean]
31
+ def does_not_match?(subscription)
32
+ !match(subscription)
33
+ end
34
+
35
+ private
36
+
37
+ def match(subscription)
38
+ case subscription
39
+ when ::ActionCable::Channel::Base
40
+ @actual = subscription.streams
41
+ no_expected? ? actual.any? : actual.any? { |i| expected === i }
42
+ else
43
+ raise ArgumentError, "have_stream, have_stream_from and have_stream_from support expectations on subscription only"
44
+ end
45
+ end
46
+
47
+ def base_message
48
+ no_expected? ? "any stream started" : "stream #{expected_formatted} started, but have #{actual_formatted}"
49
+ end
50
+
51
+ def no_expected?
52
+ !defined?(@expected)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,65 @@
1
+ require "rspec/rails/matchers/action_cable/have_broadcasted_to"
2
+
3
+ module RSpec
4
+ module Rails
5
+ module Matchers
6
+ # Namespace for various implementations of ActionCable features
7
+ #
8
+ # @api private
9
+ module ActionCable
10
+ end
11
+
12
+ # @api public
13
+ # Passes if a message has been sent to a stream/object inside a block.
14
+ # May chain `at_least`, `at_most` or `exactly` to specify a number of times.
15
+ # To specify channel from which message has been broadcasted to object use `from_channel`.
16
+ #
17
+ #
18
+ # @example
19
+ # expect {
20
+ # ActionCable.server.broadcast "messages", text: 'Hi!'
21
+ # }.to have_broadcasted_to("messages")
22
+ #
23
+ # expect {
24
+ # SomeChannel.broadcast_to(user)
25
+ # }.to have_broadcasted_to(user).from_channel(SomeChannel)
26
+ #
27
+ # # Using alias
28
+ # expect {
29
+ # ActionCable.server.broadcast "messages", text: 'Hi!'
30
+ # }.to broadcast_to("messages")
31
+ #
32
+ # expect {
33
+ # ActionCable.server.broadcast "messages", text: 'Hi!'
34
+ # ActionCable.server.broadcast "all", text: 'Hi!'
35
+ # }.to have_broadcasted_to("messages").exactly(:once)
36
+ #
37
+ # expect {
38
+ # 3.times { ActionCable.server.broadcast "messages", text: 'Hi!' }
39
+ # }.to have_broadcasted_to("messages").at_least(2).times
40
+ #
41
+ # expect {
42
+ # ActionCable.server.broadcast "messages", text: 'Hi!'
43
+ # }.to have_broadcasted_to("messages").at_most(:twice)
44
+ #
45
+ # expect {
46
+ # ActionCable.server.broadcast "messages", text: 'Hi!'
47
+ # }.to have_broadcasted_to("messages").with(text: 'Hi!')
48
+ def have_broadcasted_to(target = nil)
49
+ check_action_cable_adapter
50
+
51
+ ActionCable::HaveBroadcastedTo.new(target, channel: described_class)
52
+ end
53
+ alias_method :broadcast_to, :have_broadcasted_to
54
+
55
+ private
56
+
57
+ # @private
58
+ def check_action_cable_adapter
59
+ return if ::ActionCable::SubscriptionAdapter::Test === ::ActionCable.server.pubsub
60
+
61
+ raise StandardError, "To use ActionCable matchers set `adapter: test` in your cable.yml"
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,64 @@
1
+ module RSpec
2
+ module Rails
3
+ module Matchers
4
+ # Namespace for various implementations of ActionMailbox features
5
+ #
6
+ # @api private
7
+ module ActionMailbox
8
+ # @private
9
+ class Base < RSpec::Rails::Matchers::BaseMatcher
10
+ private
11
+
12
+ def create_inbound_email(message)
13
+ RSpec::Rails::MailboxExampleGroup.create_inbound_email(message)
14
+ end
15
+ end
16
+
17
+ # @private
18
+ class ReceiveInboundEmail < Base
19
+ def initialize(message)
20
+ super()
21
+
22
+ @inbound_email = create_inbound_email(message)
23
+ end
24
+
25
+ def matches?(mailbox)
26
+ @mailbox = mailbox
27
+ @receiver = ApplicationMailbox.router.send(:match_to_mailbox, inbound_email)
28
+
29
+ @receiver == @mailbox
30
+ end
31
+
32
+ def failure_message
33
+ "expected #{describe_inbound_email} to route to #{mailbox}".tap do |msg|
34
+ if receiver
35
+ msg << ", but routed to #{receiver} instead"
36
+ end
37
+ end
38
+ end
39
+
40
+ def failure_message_when_negated
41
+ "expected #{describe_inbound_email} not to route to #{mailbox}"
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :inbound_email, :mailbox, :receiver
47
+
48
+ def describe_inbound_email
49
+ "mail to #{inbound_email.mail.to.to_sentence}"
50
+ end
51
+ end
52
+ end
53
+
54
+ # @api public
55
+ # Passes if the given inbound email would be routed to the subject inbox.
56
+ #
57
+ # @param message [Hash, Mail::Message] a mail message or hash of
58
+ # attributes used to build one
59
+ def receive_inbound_email(message)
60
+ ActionMailbox::ReceiveInboundEmail.new(message)
61
+ end
62
+ end
63
+ end
64
+ end