rspec-rails 3.0.2 → 5.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/.document +1 -1
- data/.yardopts +3 -1
- data/Capybara.md +6 -55
- data/Changelog.md +619 -43
- data/{License.txt → LICENSE.md} +5 -3
- data/README.md +278 -444
- data/lib/generators/rspec/channel/channel_generator.rb +12 -0
- data/lib/generators/rspec/{observer/templates/observer_spec.rb → channel/templates/channel_spec.rb.erb} +1 -1
- data/lib/generators/rspec/controller/controller_generator.rb +22 -5
- data/lib/generators/rspec/controller/templates/controller_spec.rb +3 -3
- data/lib/generators/rspec/controller/templates/request_spec.rb +19 -0
- data/lib/generators/rspec/controller/templates/routing_spec.rb +13 -0
- data/lib/generators/rspec/controller/templates/view_spec.rb +1 -1
- data/lib/generators/rspec/feature/feature_generator.rb +15 -2
- data/lib/generators/rspec/feature/templates/feature_singular_spec.rb +5 -0
- data/lib/generators/rspec/feature/templates/feature_spec.rb +1 -1
- data/lib/generators/rspec/generator/generator_generator.rb +24 -0
- data/lib/generators/rspec/generator/templates/generator_spec.rb +6 -0
- data/lib/generators/rspec/helper/helper_generator.rb +1 -1
- data/lib/generators/rspec/helper/templates/helper_spec.rb +1 -1
- data/lib/generators/rspec/install/install_generator.rb +22 -5
- data/lib/generators/rspec/install/templates/spec/rails_helper.rb +43 -14
- data/lib/generators/rspec/integration/integration_generator.rb +7 -2
- data/lib/generators/rspec/integration/templates/request_spec.rb +3 -3
- data/lib/generators/rspec/job/job_generator.rb +13 -0
- data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
- data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
- data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
- data/lib/generators/rspec/mailer/mailer_generator.rb +8 -1
- data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -2
- data/lib/generators/rspec/mailer/templates/preview.rb +13 -0
- data/lib/generators/rspec/model/model_generator.rb +20 -6
- data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
- data/lib/generators/rspec/model/templates/model_spec.rb +1 -1
- data/lib/generators/rspec/request/request_generator.rb +10 -0
- data/lib/generators/rspec/scaffold/scaffold_generator.rb +90 -113
- data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +129 -0
- data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
- data/lib/generators/rspec/scaffold/templates/controller_spec.rb +46 -64
- data/lib/generators/rspec/scaffold/templates/edit_spec.rb +3 -3
- data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -3
- data/lib/generators/rspec/scaffold/templates/new_spec.rb +3 -3
- data/lib/generators/rspec/scaffold/templates/request_spec.rb +138 -0
- data/lib/generators/rspec/scaffold/templates/routing_spec.rb +18 -11
- data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
- data/lib/generators/rspec/system/system_generator.rb +24 -0
- data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
- data/lib/generators/rspec/view/templates/view_spec.rb +1 -1
- data/lib/generators/rspec/view/view_generator.rb +2 -2
- data/lib/generators/rspec.rb +12 -10
- data/lib/rspec/rails/active_record.rb +25 -0
- data/lib/rspec/rails/adapters.rb +34 -28
- data/lib/rspec/rails/configuration.rb +135 -40
- data/lib/rspec/rails/example/channel_example_group.rb +93 -0
- data/lib/rspec/rails/example/controller_example_group.rb +185 -149
- data/lib/rspec/rails/example/feature_example_group.rb +43 -23
- data/lib/rspec/rails/example/helper_example_group.rb +28 -25
- data/lib/rspec/rails/example/job_example_group.rb +23 -0
- data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
- data/lib/rspec/rails/example/mailer_example_group.rb +27 -22
- data/lib/rspec/rails/example/model_example_group.rb +9 -6
- data/lib/rspec/rails/example/rails_example_group.rb +3 -2
- data/lib/rspec/rails/example/request_example_group.rb +21 -17
- data/lib/rspec/rails/example/routing_example_group.rb +49 -39
- data/lib/rspec/rails/example/system_example_group.rb +125 -0
- data/lib/rspec/rails/example/view_example_group.rb +178 -134
- data/lib/rspec/rails/example.rb +4 -0
- data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -11
- data/lib/rspec/rails/feature_check.rb +47 -0
- data/lib/rspec/rails/file_fixture_support.rb +15 -0
- data/lib/rspec/rails/fixture_file_upload_support.rb +56 -0
- data/lib/rspec/rails/fixture_support.rb +42 -13
- data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
- data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
- data/lib/rspec/rails/matchers/action_cable.rb +65 -0
- data/lib/rspec/rails/matchers/action_mailbox.rb +73 -0
- data/lib/rspec/rails/matchers/active_job.rb +465 -0
- data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
- data/lib/rspec/rails/matchers/be_a_new.rb +70 -64
- data/lib/rspec/rails/matchers/be_new_record.rb +25 -20
- data/lib/rspec/rails/matchers/be_valid.rb +39 -34
- data/lib/rspec/rails/matchers/have_enqueued_mail.rb +226 -0
- data/lib/rspec/rails/matchers/have_http_status.rb +363 -333
- data/lib/rspec/rails/matchers/have_rendered.rb +55 -32
- data/lib/rspec/rails/matchers/redirect_to.rb +30 -27
- data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
- data/lib/rspec/rails/matchers/routing_matchers.rb +107 -101
- data/lib/rspec/rails/matchers.rb +20 -12
- data/lib/rspec/rails/tasks/rspec.rake +7 -17
- data/lib/rspec/rails/vendor/capybara.rb +12 -11
- data/lib/rspec/rails/version.rb +1 -1
- data/lib/rspec/rails/view_assigns.rb +1 -2
- data/lib/rspec/rails/view_path_builder.rb +29 -0
- data/lib/rspec/rails/view_rendering.rb +87 -27
- data/lib/rspec/rails/view_spec_methods.rb +56 -0
- data/lib/rspec/rails.rb +9 -1
- data/lib/rspec-rails.rb +62 -3
- data.tar.gz.sig +0 -0
- metadata +118 -72
- metadata.gz.sig +0 -0
- data/lib/generators/rspec/observer/observer_generator.rb +0 -13
@@ -2,16 +2,23 @@ require 'action_view/testing/resolvers'
|
|
2
2
|
|
3
3
|
module RSpec
|
4
4
|
module Rails
|
5
|
+
# @api public
|
5
6
|
# Helpers for optionally rendering views in controller specs.
|
6
7
|
module ViewRendering
|
7
8
|
extend ActiveSupport::Concern
|
8
9
|
|
9
|
-
|
10
|
+
# @!attribute [r]
|
11
|
+
# Returns the controller object instance under test.
|
12
|
+
attr_reader :controller
|
13
|
+
|
14
|
+
# @private
|
15
|
+
attr_writer :controller
|
16
|
+
private :controller=
|
10
17
|
|
11
18
|
# DSL methods
|
12
19
|
module ClassMethods
|
13
20
|
# @see RSpec::Rails::ControllerExampleGroup
|
14
|
-
def render_views(true_or_false=true)
|
21
|
+
def render_views(true_or_false = true)
|
15
22
|
@render_views = true_or_false
|
16
23
|
end
|
17
24
|
|
@@ -32,35 +39,83 @@ module RSpec
|
|
32
39
|
self.class.render_views? || !controller.class.respond_to?(:view_paths)
|
33
40
|
end
|
34
41
|
|
35
|
-
# Delegates find_all to the submitted path set and then returns templates
|
36
|
-
# with modified source
|
37
|
-
#
|
38
42
|
# @private
|
39
|
-
class
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
43
|
+
class EmptyTemplateResolver
|
44
|
+
def self.build(path)
|
45
|
+
if path.is_a?(::ActionView::Resolver)
|
46
|
+
ResolverDecorator.new(path)
|
47
|
+
else
|
48
|
+
FileSystemResolver.new(path)
|
49
|
+
end
|
44
50
|
end
|
45
51
|
|
46
|
-
def
|
47
|
-
|
52
|
+
def self.nullify_template_rendering(templates)
|
53
|
+
templates.map do |template|
|
48
54
|
::ActionView::Template.new(
|
49
55
|
"",
|
50
56
|
template.identifier,
|
51
57
|
EmptyTemplateHandler,
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
}
|
58
|
+
virtual_path: template.virtual_path,
|
59
|
+
format: template_format(template),
|
60
|
+
locals: []
|
56
61
|
)
|
57
62
|
end
|
58
63
|
end
|
64
|
+
|
65
|
+
if ::Rails::VERSION::STRING >= '6'
|
66
|
+
def self.template_format(template)
|
67
|
+
template.format
|
68
|
+
end
|
69
|
+
else
|
70
|
+
def self.template_format(template)
|
71
|
+
template.formats
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Delegates all methods to the submitted resolver and for all methods
|
76
|
+
# that return a collection of `ActionView::Template` instances, return
|
77
|
+
# templates with modified source
|
78
|
+
#
|
79
|
+
# @private
|
80
|
+
class ResolverDecorator
|
81
|
+
def initialize(resolver)
|
82
|
+
@resolver = resolver
|
83
|
+
end
|
84
|
+
|
85
|
+
def method_missing(name, *args, &block)
|
86
|
+
result = @resolver.send(name, *args, &block)
|
87
|
+
nullify_templates(result)
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def nullify_templates(collection)
|
93
|
+
return collection unless collection.is_a?(Enumerable)
|
94
|
+
return collection unless collection.all? { |element| element.is_a?(::ActionView::Template) }
|
95
|
+
|
96
|
+
EmptyTemplateResolver.nullify_template_rendering(collection)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Delegates find_templates to the submitted path set and then returns
|
101
|
+
# templates with modified source
|
102
|
+
#
|
103
|
+
# @private
|
104
|
+
class FileSystemResolver < ::ActionView::FileSystemResolver
|
105
|
+
private
|
106
|
+
|
107
|
+
def find_templates(*args)
|
108
|
+
templates = super
|
109
|
+
EmptyTemplateResolver.nullify_template_rendering(templates)
|
110
|
+
end
|
111
|
+
end
|
59
112
|
end
|
60
113
|
|
61
114
|
# @private
|
62
115
|
class EmptyTemplateHandler
|
63
|
-
def self.call(
|
116
|
+
def self.call(_template, _source = nil)
|
117
|
+
::Rails.logger.info(" Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.")
|
118
|
+
|
64
119
|
%("")
|
65
120
|
end
|
66
121
|
end
|
@@ -70,33 +125,38 @@ module RSpec
|
|
70
125
|
# @private
|
71
126
|
module EmptyTemplates
|
72
127
|
def prepend_view_path(new_path)
|
73
|
-
lookup_context.view_paths.unshift(*_path_decorator(new_path))
|
128
|
+
lookup_context.view_paths.unshift(*_path_decorator(*new_path))
|
74
129
|
end
|
75
130
|
|
76
131
|
def append_view_path(new_path)
|
77
|
-
lookup_context.view_paths.push(*_path_decorator(new_path))
|
132
|
+
lookup_context.view_paths.push(*_path_decorator(*new_path))
|
78
133
|
end
|
79
134
|
|
80
|
-
|
135
|
+
private
|
81
136
|
|
82
|
-
def _path_decorator(
|
83
|
-
|
137
|
+
def _path_decorator(*paths)
|
138
|
+
paths.map { |path| EmptyTemplateResolver.build(path) }
|
84
139
|
end
|
85
140
|
end
|
86
141
|
|
142
|
+
# @private
|
143
|
+
RESOLVER_CACHE = Hash.new do |hash, path|
|
144
|
+
hash[path] = EmptyTemplateResolver.build(path)
|
145
|
+
end
|
146
|
+
|
87
147
|
included do
|
88
148
|
before do
|
89
149
|
unless render_views?
|
90
|
-
@
|
91
|
-
|
150
|
+
@_original_path_set = controller.class.view_paths
|
151
|
+
path_set = @_original_path_set.map { |resolver| RESOLVER_CACHE[resolver] }
|
152
|
+
|
153
|
+
controller.class.view_paths = path_set
|
92
154
|
controller.extend(EmptyTemplates)
|
93
155
|
end
|
94
156
|
end
|
95
157
|
|
96
158
|
after do
|
97
|
-
unless render_views?
|
98
|
-
controller.class.view_paths = @_empty_view_path_set_delegator.original_path_set
|
99
|
-
end
|
159
|
+
controller.class.view_paths = @_original_path_set unless render_views?
|
100
160
|
end
|
101
161
|
end
|
102
162
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Rails
|
3
|
+
# Adds methods (generally to ActionView::TestCase::TestController).
|
4
|
+
# Intended for use in view specs.
|
5
|
+
module ViewSpecMethods
|
6
|
+
module_function
|
7
|
+
|
8
|
+
# Adds methods `extra_params=` and `extra_params` to the indicated class.
|
9
|
+
# When class is `::ActionView::TestCase::TestController`, these methods
|
10
|
+
# are exposed in view specs on the `controller` object.
|
11
|
+
def add_to(klass)
|
12
|
+
return if klass.method_defined?(:extra_params) && klass.method_defined?(:extra_params=)
|
13
|
+
|
14
|
+
klass.module_exec do
|
15
|
+
# Set any extra parameters that rendering a URL for this view
|
16
|
+
# would require.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
#
|
20
|
+
# # In "spec/views/widgets/show.html.erb_spec.rb":
|
21
|
+
# before do
|
22
|
+
# widget = Widget.create!(:name => "slicer")
|
23
|
+
# controller.extra_params = { :id => widget.id }
|
24
|
+
# end
|
25
|
+
def extra_params=(hash)
|
26
|
+
@extra_params = hash
|
27
|
+
request.path =
|
28
|
+
ViewPathBuilder.new(::Rails.application.routes).path_for(
|
29
|
+
extra_params.merge(request.path_parameters)
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Use to read extra parameters that are set in the view spec.
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
#
|
37
|
+
# # After the before in the above example:
|
38
|
+
# controller.extra_params
|
39
|
+
# # => { :id => 4 }
|
40
|
+
def extra_params
|
41
|
+
@extra_params ||= {}
|
42
|
+
@extra_params.dup.freeze
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Removes methods `extra_params=` and `extra_params` from the indicated class.
|
48
|
+
def remove_from(klass)
|
49
|
+
klass.module_exec do
|
50
|
+
undef extra_params= if klass.method_defined?(:extra_params=)
|
51
|
+
undef extra_params if klass.method_defined?(:extra_params)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/rspec/rails.rb
CHANGED
@@ -1,10 +1,18 @@
|
|
1
1
|
require 'rspec/core'
|
2
2
|
require 'rails/version'
|
3
|
+
|
4
|
+
# Load any of our adapters and extensions early in the process
|
5
|
+
require 'rspec/rails/adapters'
|
3
6
|
require 'rspec/rails/extensions'
|
7
|
+
|
8
|
+
# Load the rspec-rails parts
|
4
9
|
require 'rspec/rails/view_rendering'
|
5
|
-
require 'rspec/rails/adapters'
|
6
10
|
require 'rspec/rails/matchers'
|
7
11
|
require 'rspec/rails/fixture_support'
|
12
|
+
require 'rspec/rails/file_fixture_support'
|
13
|
+
require 'rspec/rails/fixture_file_upload_support'
|
8
14
|
require 'rspec/rails/example'
|
9
15
|
require 'rspec/rails/vendor/capybara'
|
10
16
|
require 'rspec/rails/configuration'
|
17
|
+
require 'rspec/rails/active_record'
|
18
|
+
require 'rspec/rails/feature_check'
|
data/lib/rspec-rails.rb
CHANGED
@@ -1,17 +1,76 @@
|
|
1
|
+
require 'rspec/rails/feature_check'
|
2
|
+
|
1
3
|
# Namespace for all core RSpec projects.
|
2
4
|
module RSpec
|
3
5
|
# Namespace for rspec-rails code.
|
4
6
|
module Rails
|
5
7
|
# Railtie to hook into Rails.
|
6
8
|
class Railtie < ::Rails::Railtie
|
7
|
-
# Rails
|
8
|
-
|
9
|
+
# As of Rails 5.1.0 you can register directories to work with `rake notes`
|
10
|
+
require 'rails/source_annotation_extractor'
|
11
|
+
if ::Rails::VERSION::STRING >= '6.0'
|
12
|
+
::Rails::SourceAnnotationExtractor::Annotation.register_directories("spec")
|
13
|
+
else
|
14
|
+
SourceAnnotationExtractor::Annotation.register_directories("spec")
|
15
|
+
end
|
16
|
+
generators = config.app_generators
|
9
17
|
generators.integration_tool :rspec
|
10
|
-
generators.test_framework
|
18
|
+
generators.test_framework :rspec
|
19
|
+
|
20
|
+
generators do
|
21
|
+
::Rails::Generators.hidden_namespaces.reject! { |namespace| namespace.to_s.start_with?("rspec") }
|
22
|
+
end
|
11
23
|
|
12
24
|
rake_tasks do
|
13
25
|
load "rspec/rails/tasks/rspec.rake"
|
14
26
|
end
|
27
|
+
|
28
|
+
# This is called after the environment has been loaded but before Rails
|
29
|
+
# sets the default for the `preview_path`
|
30
|
+
initializer "rspec_rails.action_mailer",
|
31
|
+
before: "action_mailer.set_configs" do |app|
|
32
|
+
setup_preview_path(app)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def setup_preview_path(app)
|
38
|
+
return unless supports_action_mailer_previews?(app.config)
|
39
|
+
|
40
|
+
options = app.config.action_mailer
|
41
|
+
config_default_preview_path(options) if config_preview_path?(options)
|
42
|
+
end
|
43
|
+
|
44
|
+
def config_preview_path?(options)
|
45
|
+
# We cannot use `respond_to?(:show_previews)` here as it will always
|
46
|
+
# return `true`.
|
47
|
+
if options.show_previews.nil?
|
48
|
+
options.show_previews = ::Rails.env.development?
|
49
|
+
else
|
50
|
+
options.show_previews
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def config_default_preview_path(options)
|
55
|
+
return unless options.preview_path.blank?
|
56
|
+
|
57
|
+
options.preview_path = "#{::Rails.root}/spec/mailers/previews"
|
58
|
+
end
|
59
|
+
|
60
|
+
def supports_action_mailer_previews?(config)
|
61
|
+
# These checks avoid loading `ActionMailer`. Using `defined?` has the
|
62
|
+
# side-effect of the class getting loaded if it is available. This is
|
63
|
+
# problematic because loading `ActionMailer::Base` will cause it to
|
64
|
+
# read the config settings; this is the only time the config is read.
|
65
|
+
# If the config is loaded now, any settings declared in a config block
|
66
|
+
# in an initializer will be ignored.
|
67
|
+
#
|
68
|
+
# If the action mailer railtie has not been loaded then `config` will
|
69
|
+
# not respond to the method. However, we cannot use
|
70
|
+
# `config.action_mailer.respond_to?(:preview_path)` here as it will
|
71
|
+
# always return `true`.
|
72
|
+
config.respond_to?(:action_mailer)
|
73
|
+
end
|
15
74
|
end
|
16
75
|
end
|
17
76
|
end
|
data.tar.gz.sig
CHANGED
Binary file
|