rspec-rails 3.8.2 → 7.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/Capybara.md +5 -54
  4. data/Changelog.md +440 -76
  5. data/README.md +281 -500
  6. data/lib/generators/rspec/channel/channel_generator.rb +12 -0
  7. data/lib/generators/rspec/{observer/templates/observer_spec.rb → channel/templates/channel_spec.rb.erb} +1 -1
  8. data/lib/generators/rspec/controller/controller_generator.rb +24 -7
  9. data/lib/generators/rspec/controller/templates/request_spec.rb +19 -0
  10. data/lib/generators/rspec/controller/templates/routing_spec.rb +13 -0
  11. data/lib/generators/rspec/feature/feature_generator.rb +3 -3
  12. data/lib/generators/rspec/generator/generator_generator.rb +24 -0
  13. data/lib/generators/rspec/generator/templates/generator_spec.rb +5 -0
  14. data/lib/generators/rspec/helper/helper_generator.rb +2 -2
  15. data/lib/generators/rspec/install/install_generator.rb +23 -6
  16. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +32 -17
  17. data/lib/generators/rspec/job/job_generator.rb +2 -1
  18. data/lib/generators/rspec/job/templates/job_spec.rb.erb +1 -1
  19. data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
  20. data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
  21. data/lib/generators/rspec/mailer/mailer_generator.rb +7 -4
  22. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -2
  23. data/lib/generators/rspec/mailer/templates/preview.rb +4 -4
  24. data/lib/generators/rspec/model/model_generator.rb +8 -7
  25. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  26. data/lib/generators/rspec/request/request_generator.rb +10 -3
  27. data/lib/generators/rspec/scaffold/scaffold_generator.rb +36 -22
  28. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +13 -49
  29. data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
  30. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +14 -62
  31. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +9 -9
  32. data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -2
  33. data/lib/generators/rspec/scaffold/templates/new_spec.rb +2 -6
  34. data/lib/generators/rspec/scaffold/templates/request_spec.rb +138 -0
  35. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +8 -10
  36. data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
  37. data/lib/generators/rspec/system/system_generator.rb +24 -0
  38. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  39. data/lib/generators/rspec/view/view_generator.rb +4 -4
  40. data/lib/generators/rspec.rb +16 -5
  41. data/lib/rspec/rails/adapters.rb +22 -76
  42. data/lib/rspec/rails/configuration.rb +112 -38
  43. data/lib/rspec/rails/example/channel_example_group.rb +93 -0
  44. data/lib/rspec/rails/example/controller_example_group.rb +5 -4
  45. data/lib/rspec/rails/example/feature_example_group.rb +6 -26
  46. data/lib/rspec/rails/example/helper_example_group.rb +2 -9
  47. data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
  48. data/lib/rspec/rails/example/mailer_example_group.rb +2 -2
  49. data/lib/rspec/rails/example/rails_example_group.rb +7 -1
  50. data/lib/rspec/rails/example/request_example_group.rb +1 -4
  51. data/lib/rspec/rails/example/routing_example_group.rb +0 -2
  52. data/lib/rspec/rails/example/system_example_group.rb +88 -16
  53. data/lib/rspec/rails/example/view_example_group.rb +40 -28
  54. data/lib/rspec/rails/example.rb +2 -0
  55. data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -10
  56. data/lib/rspec/rails/feature_check.rb +16 -29
  57. data/lib/rspec/rails/file_fixture_support.rb +11 -10
  58. data/lib/rspec/rails/fixture_file_upload_support.rb +20 -15
  59. data/lib/rspec/rails/fixture_support.rb +64 -34
  60. data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +173 -0
  61. data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
  62. data/lib/rspec/rails/matchers/action_cable.rb +65 -0
  63. data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
  64. data/lib/rspec/rails/matchers/active_job.rb +224 -24
  65. data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
  66. data/lib/rspec/rails/matchers/be_a_new.rb +1 -1
  67. data/lib/rspec/rails/matchers/be_new_record.rb +1 -1
  68. data/lib/rspec/rails/matchers/be_valid.rb +1 -1
  69. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +258 -0
  70. data/lib/rspec/rails/matchers/have_http_status.rb +23 -32
  71. data/lib/rspec/rails/matchers/have_rendered.rb +2 -1
  72. data/lib/rspec/rails/matchers/redirect_to.rb +1 -1
  73. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  74. data/lib/rspec/rails/matchers/routing_matchers.rb +13 -13
  75. data/lib/rspec/rails/matchers/send_email.rb +122 -0
  76. data/lib/rspec/rails/matchers.rb +12 -0
  77. data/lib/rspec/rails/tasks/rspec.rake +9 -17
  78. data/lib/rspec/rails/vendor/capybara.rb +10 -17
  79. data/lib/rspec/rails/version.rb +1 -1
  80. data/lib/rspec/rails/view_assigns.rb +0 -18
  81. data/lib/rspec/rails/view_path_builder.rb +1 -1
  82. data/lib/rspec/rails/view_rendering.rb +20 -7
  83. data/lib/rspec-rails.rb +36 -18
  84. data.tar.gz.sig +0 -0
  85. metadata +55 -37
  86. metadata.gz.sig +0 -0
  87. data/lib/generators/rspec/integration/integration_generator.rb +0 -22
  88. data/lib/generators/rspec/observer/observer_generator.rb +0 -13
  89. /data/lib/generators/rspec/{integration → request}/templates/request_spec.rb +0 -0
@@ -5,52 +5,82 @@ module RSpec
5
5
  if defined?(ActiveRecord::TestFixtures)
6
6
  extend ActiveSupport::Concern
7
7
  include RSpec::Rails::SetupAndTeardownAdapter
8
- include RSpec::Rails::MinitestLifecycleAdapter if ::ActiveRecord::VERSION::STRING > '4'
8
+ include RSpec::Rails::MinitestLifecycleAdapter
9
9
  include RSpec::Rails::MinitestAssertionAdapter
10
10
  include ActiveRecord::TestFixtures
11
11
 
12
+ # @private prevent ActiveSupport::TestFixtures to start a DB transaction.
13
+ # Monkey patched to avoid collisions with 'let(:name)' since Rails 6.1
14
+ def run_in_transaction?
15
+ current_example_name = (RSpec.current_example && RSpec.current_example.metadata[:description])
16
+ use_transactional_tests && !self.class.uses_transaction?(current_example_name)
17
+ end
18
+
12
19
  included do
13
- # TODO: (DC 2011-06-25) this is necessary because fixture_file_upload
14
- # accesses fixture_path directly on ActiveSupport::TestCase. This is
15
- # fixed in rails by https://github.com/rails/rails/pull/1861, which
16
- # should be part of the 3.1 release, at which point we can include
17
- # these lines for rails < 3.1.
18
- ActiveSupport::TestCase.class_exec do
19
- include ActiveRecord::TestFixtures
20
- self.fixture_path = RSpec.configuration.fixture_path
21
- end
22
- # /TODO
20
+ if RSpec.configuration.use_active_record?
21
+ include Fixtures
22
+
23
+ # TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
24
+ if respond_to?(:fixture_paths=)
25
+ self.fixture_paths = RSpec.configuration.fixture_paths
26
+ else
27
+ self.fixture_path = RSpec.configuration.fixture_path
28
+ end
23
29
 
24
- self.fixture_path = RSpec.configuration.fixture_path
25
- if ::Rails::VERSION::STRING > '5'
26
30
  self.use_transactional_tests = RSpec.configuration.use_transactional_fixtures
27
- else
28
- self.use_transactional_fixtures = RSpec.configuration.use_transactional_fixtures
31
+ self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
32
+
33
+ fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
29
34
  end
30
- self.use_instantiated_fixtures = RSpec.configuration.use_instantiated_fixtures
31
-
32
- def self.fixtures(*args)
33
- orig_methods = private_instance_methods
34
- super.tap do
35
- new_methods = private_instance_methods - orig_methods
36
- new_methods.each do |method_name|
37
- proxy_method_warning_if_called_in_before_context_scope(method_name)
35
+ end
36
+
37
+ module Fixtures
38
+ extend ActiveSupport::Concern
39
+
40
+ # rubocop:disable Metrics/BlockLength
41
+ class_methods do
42
+ if ::Rails.version.to_f >= 7.1
43
+ def fixtures(*args)
44
+ super.tap do
45
+ fixture_sets.each_pair do |method_name, fixture_name|
46
+ proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name)
47
+ end
48
+ end
38
49
  end
39
- end
40
- end
41
50
 
42
- def self.proxy_method_warning_if_called_in_before_context_scope(method_name)
43
- orig_implementation = instance_method(method_name)
44
- define_method(method_name) do |*args, &blk|
45
- if inspect.include?("before(:context)")
46
- RSpec.warn_with("Calling fixture method in before :context ")
47
- else
48
- orig_implementation.bind(self).call(*args, &blk)
51
+ def proxy_method_warning_if_called_in_before_context_scope(method_name, fixture_name)
52
+ define_method(method_name) do |*args, **kwargs, &blk|
53
+ if RSpec.current_scope == :before_context_hook
54
+ RSpec.warn_with("Calling fixture method in before :context ")
55
+ else
56
+ access_fixture(fixture_name, *args, **kwargs, &blk)
57
+ end
58
+ end
59
+ end
60
+ else
61
+ def fixtures(*args)
62
+ orig_methods = private_instance_methods
63
+ super.tap do
64
+ new_methods = private_instance_methods - orig_methods
65
+ new_methods.each do |method_name|
66
+ proxy_method_warning_if_called_in_before_context_scope(method_name)
67
+ end
68
+ end
69
+ end
70
+
71
+ def proxy_method_warning_if_called_in_before_context_scope(method_name)
72
+ orig_implementation = instance_method(method_name)
73
+ define_method(method_name) do |*args, &blk|
74
+ if RSpec.current_scope == :before_context_hook
75
+ RSpec.warn_with("Calling fixture method in before :context ")
76
+ else
77
+ orig_implementation.bind(self).call(*args, &blk)
78
+ end
79
+ end
49
80
  end
50
81
  end
51
82
  end
52
-
53
- fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
83
+ # rubocop:enable Metrics/BlockLength
54
84
  end
55
85
  end
56
86
  end
@@ -0,0 +1,173 @@
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 ||= case @target
100
+ when String
101
+ @target
102
+ when Symbol
103
+ @target.to_s
104
+ else
105
+ check_channel_presence
106
+ @channel.broadcasting_for(@target)
107
+ end
108
+ end
109
+
110
+ def check(messages)
111
+ @matching_msgs, @unmatching_msgs = messages.partition do |msg|
112
+ decoded = ActiveSupport::JSON.decode(msg)
113
+ decoded = decoded.with_indifferent_access if decoded.is_a?(Hash)
114
+
115
+ if @data.nil? || values_match?(@data, decoded)
116
+ @block.call(decoded)
117
+ true
118
+ else
119
+ false
120
+ end
121
+ end
122
+
123
+ @matching_msgs_count = @matching_msgs.size
124
+
125
+ case @expectation_type
126
+ when :exactly then @expected_number == @matching_msgs_count
127
+ when :at_most then @expected_number >= @matching_msgs_count
128
+ when :at_least then @expected_number <= @matching_msgs_count
129
+ end
130
+ end
131
+
132
+ def set_expected_number(relativity, count)
133
+ @expectation_type = relativity
134
+ @expected_number =
135
+ case count
136
+ when :once then 1
137
+ when :twice then 2
138
+ when :thrice then 3
139
+ else Integer(count)
140
+ end
141
+ end
142
+
143
+ def base_message
144
+ "#{message_expectation_modifier} #{@expected_number} messages to #{stream}".tap do |msg|
145
+ msg << " with #{data_description(@data)}" unless @data.nil?
146
+ msg << ", but broadcast #{@matching_msgs_count}"
147
+ end
148
+ end
149
+
150
+ def data_description(data)
151
+ if data.is_a?(RSpec::Matchers::Composable)
152
+ data.description
153
+ else
154
+ data
155
+ end
156
+ end
157
+
158
+ def pubsub_adapter
159
+ ::ActionCable.server.pubsub
160
+ end
161
+
162
+ def check_channel_presence
163
+ return if @channel.present? && @channel.respond_to?(:channel_name)
164
+
165
+ error_msg = "Broadcasting channel can't be inferred. Please, specify it with `from_channel`"
166
+ raise ArgumentError, error_msg
167
+ end
168
+ end
169
+ # rubocop: enable Metrics/ClassLength
170
+ end
171
+ end
172
+ end
173
+ 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,73 @@
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
+ if defined?(::ApplicationMailbox) && ::ApplicationMailbox.router.respond_to?(:mailbox_for)
26
+ def matches?(mailbox)
27
+ @mailbox = mailbox
28
+ @receiver = ApplicationMailbox.router.mailbox_for(inbound_email)
29
+
30
+ @receiver == @mailbox
31
+ end
32
+ else
33
+ def matches?(mailbox)
34
+ @mailbox = mailbox
35
+ @receiver = ApplicationMailbox.router.send(:match_to_mailbox, inbound_email)
36
+
37
+ @receiver == @mailbox
38
+ end
39
+ end
40
+
41
+ def failure_message
42
+ "expected #{describe_inbound_email} to route to #{mailbox}".tap do |msg|
43
+ if receiver
44
+ msg << ", but routed to #{receiver} instead"
45
+ end
46
+ end
47
+ end
48
+
49
+ def failure_message_when_negated
50
+ "expected #{describe_inbound_email} not to route to #{mailbox}"
51
+ end
52
+
53
+ private
54
+
55
+ attr_reader :inbound_email, :mailbox, :receiver
56
+
57
+ def describe_inbound_email
58
+ "mail to #{inbound_email.mail.to.to_sentence}"
59
+ end
60
+ end
61
+ end
62
+
63
+ # @api public
64
+ # Passes if the given inbound email would be routed to the subject inbox.
65
+ #
66
+ # @param message [Hash, Mail::Message] a mail message or hash of
67
+ # attributes used to build one
68
+ def receive_inbound_email(message)
69
+ ActionMailbox::ReceiveInboundEmail.new(message)
70
+ end
71
+ end
72
+ end
73
+ end