rspec-rails 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Changelog.md +17 -0
  5. data/README.md +7 -7
  6. data/lib/generators/rspec.rb +10 -6
  7. data/lib/generators/rspec/controller/templates/controller_spec.rb +1 -1
  8. data/lib/generators/rspec/install/install_generator.rb +19 -2
  9. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +13 -4
  10. data/lib/generators/rspec/integration/templates/request_spec.rb +1 -1
  11. data/lib/generators/rspec/job/job_generator.rb +12 -0
  12. data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
  13. data/lib/generators/rspec/model/model_generator.rb +18 -5
  14. data/lib/generators/rspec/scaffold/scaffold_generator.rb +107 -100
  15. data/lib/rspec-rails.rb +1 -1
  16. data/lib/rspec/rails.rb +1 -0
  17. data/lib/rspec/rails/adapters.rb +9 -7
  18. data/lib/rspec/rails/configuration.rb +2 -3
  19. data/lib/rspec/rails/example/controller_example_group.rb +172 -149
  20. data/lib/rspec/rails/example/feature_example_group.rb +25 -23
  21. data/lib/rspec/rails/example/helper_example_group.rb +27 -25
  22. data/lib/rspec/rails/example/mailer_example_group.rb +26 -22
  23. data/lib/rspec/rails/example/model_example_group.rb +8 -6
  24. data/lib/rspec/rails/example/request_example_group.rb +19 -17
  25. data/lib/rspec/rails/example/routing_example_group.rb +40 -38
  26. data/lib/rspec/rails/example/view_example_group.rb +140 -137
  27. data/lib/rspec/rails/extensions/active_record/proxy.rb +0 -1
  28. data/lib/rspec/rails/feature_check.rb +35 -0
  29. data/lib/rspec/rails/fixture_support.rb +1 -1
  30. data/lib/rspec/rails/matchers.rb +5 -2
  31. data/lib/rspec/rails/matchers/be_a_new.rb +68 -64
  32. data/lib/rspec/rails/matchers/be_new_record.rb +24 -20
  33. data/lib/rspec/rails/matchers/be_valid.rb +38 -34
  34. data/lib/rspec/rails/matchers/have_http_status.rb +340 -334
  35. data/lib/rspec/rails/matchers/have_rendered.rb +35 -32
  36. data/lib/rspec/rails/matchers/redirect_to.rb +30 -27
  37. data/lib/rspec/rails/matchers/routing_matchers.rb +103 -101
  38. data/lib/rspec/rails/version.rb +1 -1
  39. data/lib/rspec/rails/view_assigns.rb +1 -2
  40. data/lib/rspec/rails/view_rendering.rb +6 -8
  41. metadata +16 -13
  42. metadata.gz.sig +3 -2
@@ -1,38 +1,40 @@
1
1
  require 'rspec/rails/view_assigns'
2
2
 
3
- module RSpec::Rails
4
- # Container module for helper specs.
5
- module HelperExampleGroup
6
- extend ActiveSupport::Concern
7
- include RSpec::Rails::RailsExampleGroup
8
- include ActionView::TestCase::Behavior
9
- include RSpec::Rails::ViewAssigns
3
+ module RSpec
4
+ module Rails
5
+ # Container module for helper specs.
6
+ module HelperExampleGroup
7
+ extend ActiveSupport::Concern
8
+ include RSpec::Rails::RailsExampleGroup
9
+ include ActionView::TestCase::Behavior
10
+ include RSpec::Rails::ViewAssigns
10
11
 
11
- # @private
12
- module ClassMethods
13
- def determine_default_helper_class(ignore)
14
- described_class
12
+ # @private
13
+ module ClassMethods
14
+ def determine_default_helper_class(_ignore)
15
+ described_class
16
+ end
15
17
  end
16
- end
17
18
 
18
- # Returns an instance of ActionView::Base with the helper being specified
19
- # mixed in, along with any of the built-in rails helpers.
20
- def helper
21
- _view.tap do |v|
22
- v.extend(ApplicationHelper) if defined?(ApplicationHelper)
23
- v.assign(view_assigns)
19
+ # Returns an instance of ActionView::Base with the helper being specified
20
+ # mixed in, along with any of the built-in rails helpers.
21
+ def helper
22
+ _view.tap do |v|
23
+ v.extend(ApplicationHelper) if defined?(ApplicationHelper)
24
+ v.assign(view_assigns)
25
+ end
24
26
  end
25
- end
26
27
 
27
28
  private
28
29
 
29
- def _controller_path(example)
30
- example.example_group.described_class.to_s.sub(/Helper/,'').underscore
31
- end
30
+ def _controller_path(example)
31
+ example.example_group.described_class.to_s.sub(/Helper/, '').underscore
32
+ end
32
33
 
33
- included do
34
- before do |example|
35
- controller.controller_path = _controller_path(example)
34
+ included do
35
+ before do |example|
36
+ controller.controller_path = _controller_path(example)
37
+ end
36
38
  end
37
39
  end
38
40
  end
@@ -1,31 +1,35 @@
1
- module RSpec::Rails
2
- # Container module for mailer spec functionality. It is only available if
3
- # ActionMailer has been loaded before it.
4
- module MailerExampleGroup
5
- # This blank module is only necessary for YARD processing. It doesn't
6
- # handle the conditional `defined?` check below very well.
1
+ module RSpec
2
+ module Rails
3
+ # Container module for mailer spec functionality. It is only available if
4
+ # ActionMailer has been loaded before it.
5
+ module MailerExampleGroup
6
+ # This blank module is only necessary for YARD processing. It doesn't
7
+ # handle the conditional `defined?` check below very well.
8
+ end
7
9
  end
8
10
  end
9
11
 
10
12
  if defined?(ActionMailer)
11
- module RSpec::Rails
12
- # Container module for mailer spec functionality.
13
- module MailerExampleGroup
14
- extend ActiveSupport::Concern
15
- include RSpec::Rails::RailsExampleGroup
16
- include ActionMailer::TestCase::Behavior
13
+ module RSpec
14
+ module Rails
15
+ # Container module for mailer spec functionality.
16
+ module MailerExampleGroup
17
+ extend ActiveSupport::Concern
18
+ include RSpec::Rails::RailsExampleGroup
19
+ include ActionMailer::TestCase::Behavior
17
20
 
18
- included do
19
- include ::Rails.application.routes.url_helpers
20
- options = ::Rails.configuration.action_mailer.default_url_options
21
- options.each { |key, value| default_url_options[key] = value } if options
22
- end
21
+ included do
22
+ include ::Rails.application.routes.url_helpers
23
+ options = ::Rails.configuration.action_mailer.default_url_options
24
+ options.each { |key, value| default_url_options[key] = value } if options
25
+ end
23
26
 
24
- # Class-level DSL for mailer specs.
25
- module ClassMethods
26
- # Alias for `described_class`.
27
- def mailer_class
28
- described_class
27
+ # Class-level DSL for mailer specs.
28
+ module ClassMethods
29
+ # Alias for `described_class`.
30
+ def mailer_class
31
+ described_class
32
+ end
29
33
  end
30
34
  end
31
35
  end
@@ -1,8 +1,10 @@
1
- module RSpec::Rails
2
- # Container class for model spec functionality. Does not provide anything
3
- # special over the common RailsExampleGroup currently.
4
- module ModelExampleGroup
5
- extend ActiveSupport::Concern
6
- include RSpec::Rails::RailsExampleGroup
1
+ module RSpec
2
+ module Rails
3
+ # Container class for model spec functionality. Does not provide anything
4
+ # special over the common RailsExampleGroup currently.
5
+ module ModelExampleGroup
6
+ extend ActiveSupport::Concern
7
+ include RSpec::Rails::RailsExampleGroup
8
+ end
7
9
  end
8
10
  end
@@ -1,22 +1,24 @@
1
- module RSpec::Rails
2
- # Container class for request spec functionality.
3
- module RequestExampleGroup
4
- extend ActiveSupport::Concern
5
- include RSpec::Rails::RailsExampleGroup
6
- include ActionDispatch::Integration::Runner
7
- include ActionDispatch::Assertions
8
- include RSpec::Rails::Matchers::RedirectTo
9
- include RSpec::Rails::Matchers::RenderTemplate
10
- include ActionController::TemplateAssertions
1
+ module RSpec
2
+ module Rails
3
+ # Container class for request spec functionality.
4
+ module RequestExampleGroup
5
+ extend ActiveSupport::Concern
6
+ include RSpec::Rails::RailsExampleGroup
7
+ include ActionDispatch::Integration::Runner
8
+ include ActionDispatch::Assertions
9
+ include RSpec::Rails::Matchers::RedirectTo
10
+ include RSpec::Rails::Matchers::RenderTemplate
11
+ include ActionController::TemplateAssertions
11
12
 
12
- # Delegates to `Rails.application`.
13
- def app
14
- ::Rails.application
15
- end
13
+ # Delegates to `Rails.application`.
14
+ def app
15
+ ::Rails.application
16
+ end
16
17
 
17
- included do
18
- before do
19
- @routes = ::Rails.application.routes
18
+ included do
19
+ before do
20
+ @routes = ::Rails.application.routes
21
+ end
20
22
  end
21
23
  end
22
24
  end
@@ -1,53 +1,55 @@
1
1
  require "action_dispatch/testing/assertions/routing"
2
2
 
3
- module RSpec::Rails
4
- # Container module for routing spec functionality.
5
- module RoutingExampleGroup
6
- extend ActiveSupport::Concern
7
- include RSpec::Rails::RailsExampleGroup
8
- include RSpec::Rails::Matchers::RoutingMatchers
9
- include RSpec::Rails::Matchers::RoutingMatchers::RouteHelpers
10
- include RSpec::Rails::AssertionDelegator.new(ActionDispatch::Assertions::RoutingAssertions)
3
+ module RSpec
4
+ module Rails
5
+ # Container module for routing spec functionality.
6
+ module RoutingExampleGroup
7
+ extend ActiveSupport::Concern
8
+ include RSpec::Rails::RailsExampleGroup
9
+ include RSpec::Rails::Matchers::RoutingMatchers
10
+ include RSpec::Rails::Matchers::RoutingMatchers::RouteHelpers
11
+ include RSpec::Rails::AssertionDelegator.new(ActionDispatch::Assertions::RoutingAssertions)
11
12
 
12
- # Class-level DSL for route specs.
13
- module ClassMethods
14
- # Specifies the routeset that will be used for the example group. This
15
- # is most useful when testing Rails engines.
16
- #
17
- # @example
18
- # describe MyEngine::PostsController do
19
- # routes { MyEngine::Engine.routes }
20
- #
21
- # it "routes posts#index" do
22
- # expect(:get => "/posts").to
23
- # route_to(:controller => "my_engine/posts", :action => "index")
24
- # end
25
- # end
26
- def routes(&blk)
27
- before do
28
- self.routes = blk.call
13
+ # Class-level DSL for route specs.
14
+ module ClassMethods
15
+ # Specifies the routeset that will be used for the example group. This
16
+ # is most useful when testing Rails engines.
17
+ #
18
+ # @example
19
+ # describe MyEngine::PostsController do
20
+ # routes { MyEngine::Engine.routes }
21
+ #
22
+ # it "routes posts#index" do
23
+ # expect(:get => "/posts").to
24
+ # route_to(:controller => "my_engine/posts", :action => "index")
25
+ # end
26
+ # end
27
+ def routes(&blk)
28
+ before do
29
+ self.routes = blk.call
30
+ end
29
31
  end
30
32
  end
31
- end
32
33
 
33
- included do
34
- before do
35
- self.routes = ::Rails.application.routes
34
+ included do
35
+ before do
36
+ self.routes = ::Rails.application.routes
37
+ end
36
38
  end
37
- end
38
39
 
39
- attr_reader :routes
40
+ attr_reader :routes
40
41
 
41
- # @private
42
- def routes=(routes)
43
- @routes = routes
44
- assertion_instance.instance_variable_set(:@routes, routes)
45
- end
42
+ # @private
43
+ def routes=(routes)
44
+ @routes = routes
45
+ assertion_instance.instance_variable_set(:@routes, routes)
46
+ end
46
47
 
47
48
  private
48
49
 
49
- def method_missing(m, *args, &block)
50
- routes.url_helpers.respond_to?(m) ? routes.url_helpers.send(m, *args) : super
50
+ def method_missing(m, *args, &block)
51
+ routes.url_helpers.respond_to?(m) ? routes.url_helpers.send(m, *args) : super
52
+ end
51
53
  end
52
54
  end
53
55
  end
@@ -1,169 +1,172 @@
1
1
  require 'rspec/rails/view_assigns'
2
2
 
3
- module RSpec::Rails
4
- # Container class for view spec functionality.
5
- module ViewExampleGroup
6
- extend ActiveSupport::Concern
7
- include RSpec::Rails::RailsExampleGroup
8
- include ActionView::TestCase::Behavior
9
- include RSpec::Rails::ViewAssigns
10
- include RSpec::Rails::Matchers::RenderTemplate
11
-
12
- # @private
13
- module ClassMethods
14
- def _default_helper
15
- base = metadata[:description].split('/')[0..-2].join('/')
16
- (base.camelize + 'Helper').constantize if base
17
- rescue NameError
18
- nil
19
- end
20
-
21
- def _default_helpers
22
- helpers = [_default_helper].compact
23
- helpers << ApplicationHelper if Object.const_defined?('ApplicationHelper')
24
- helpers
25
- end
26
- end
3
+ module RSpec
4
+ module Rails
5
+ # Container class for view spec functionality.
6
+ module ViewExampleGroup
7
+ extend ActiveSupport::Concern
8
+ include RSpec::Rails::RailsExampleGroup
9
+ include ActionView::TestCase::Behavior
10
+ include RSpec::Rails::ViewAssigns
11
+ include RSpec::Rails::Matchers::RenderTemplate
12
+
13
+ # @private
14
+ module ClassMethods
15
+ def _default_helper
16
+ base = metadata[:description].split('/')[0..-2].join('/')
17
+ (base.camelize + 'Helper').constantize if base
18
+ rescue NameError
19
+ nil
20
+ end
27
21
 
28
- # DSL exposed to view specs.
29
- module ExampleMethods
30
- # @overload render
31
- # @overload render({:partial => path_to_file})
32
- # @overload render({:partial => path_to_file}, {... locals ...})
33
- # @overload render({:partial => path_to_file}, {... locals ...}) do ... end
34
- #
35
- # Delegates to ActionView::Base#render, so see documentation on that
36
- # for more info.
37
- #
38
- # The only addition is that you can call render with no arguments, and RSpec
39
- # will pass the top level description to render:
40
- #
41
- # describe "widgets/new.html.erb" do
42
- # it "shows all the widgets" do
43
- # render # => view.render(:file => "widgets/new.html.erb")
44
- # # ...
45
- # end
46
- # end
47
- def render(options={}, local_assigns={}, &block)
48
- options = _default_render_options if Hash === options and options.empty?
49
- super(options, local_assigns, &block)
22
+ def _default_helpers
23
+ helpers = [_default_helper].compact
24
+ helpers << ApplicationHelper if Object.const_defined?('ApplicationHelper')
25
+ helpers
26
+ end
50
27
  end
51
28
 
52
- # The instance of `ActionView::Base` that is used to render the template.
53
- # Use this to stub methods _before_ calling `render`.
54
- #
55
- # describe "widgets/new.html.erb" do
56
- # it "shows all the widgets" do
57
- # view.stub(:foo) { "foo" }
58
- # render
59
- # # ...
60
- # end
61
- # end
62
- def view
63
- _view
64
- end
29
+ # DSL exposed to view specs.
30
+ module ExampleMethods
31
+ # @overload render
32
+ # @overload render({:partial => path_to_file})
33
+ # @overload render({:partial => path_to_file}, {... locals ...})
34
+ # @overload render({:partial => path_to_file}, {... locals ...}) do ... end
35
+ #
36
+ # Delegates to ActionView::Base#render, so see documentation on that
37
+ # for more info.
38
+ #
39
+ # The only addition is that you can call render with no arguments, and
40
+ # RSpec will pass the top level description to render:
41
+ #
42
+ # describe "widgets/new.html.erb" do
43
+ # it "shows all the widgets" do
44
+ # render # => view.render(:file => "widgets/new.html.erb")
45
+ # # ...
46
+ # end
47
+ # end
48
+ def render(options = {}, local_assigns = {}, &block)
49
+ options = _default_render_options if Hash === options && options.empty?
50
+ super(options, local_assigns, &block)
51
+ end
65
52
 
66
- # Simulates the presence of a template on the file system by adding a
67
- # Rails' FixtureResolver to the front of the view_paths list. Designed to
68
- # help isolate view examples from partials rendered by the view template
69
- # that is the subject of the example.
70
- #
71
- # stub_template("widgets/_widget.html.erb" => "This content.")
72
- def stub_template(hash)
73
- view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
74
- end
53
+ # The instance of `ActionView::Base` that is used to render the template.
54
+ # Use this to stub methods _before_ calling `render`.
55
+ #
56
+ # describe "widgets/new.html.erb" do
57
+ # it "shows all the widgets" do
58
+ # view.stub(:foo) { "foo" }
59
+ # render
60
+ # # ...
61
+ # end
62
+ # end
63
+ def view
64
+ _view
65
+ end
75
66
 
76
- # Provides access to the params hash that will be available within the
77
- # view.
78
- #
79
- # params[:foo] = 'bar'
80
- def params
81
- controller.params
82
- end
67
+ # Simulates the presence of a template on the file system by adding a
68
+ # Rails' FixtureResolver to the front of the view_paths list. Designed to
69
+ # help isolate view examples from partials rendered by the view template
70
+ # that is the subject of the example.
71
+ #
72
+ # stub_template("widgets/_widget.html.erb" => "This content.")
73
+ def stub_template(hash)
74
+ view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
75
+ end
83
76
 
84
- # @deprecated Use `view` instead.
85
- def template
86
- RSpec.deprecate("template", :replacement => "view")
87
- view
88
- end
77
+ # Provides access to the params hash that will be available within the
78
+ # view.
79
+ #
80
+ # params[:foo] = 'bar'
81
+ def params
82
+ controller.params
83
+ end
89
84
 
90
- # @deprecated Use `rendered` instead.
91
- def response
92
- # `assert_template` expects `response` to implement a #body method
93
- # like an `ActionDispatch::Response` does to force the view to render.
94
- # For backwards compatibility, we use #response as an alias for
95
- # #rendered, but it needs to implement #body to avoid `assert_template`
96
- # raising a `NoMethodError`.
97
- unless rendered.respond_to?(:body)
98
- def rendered.body; self; end;
85
+ # @deprecated Use `view` instead.
86
+ def template
87
+ RSpec.deprecate("template", :replacement => "view")
88
+ view
99
89
  end
100
90
 
101
- rendered
102
- end
91
+ # @deprecated Use `rendered` instead.
92
+ def response
93
+ # `assert_template` expects `response` to implement a #body method
94
+ # like an `ActionDispatch::Response` does to force the view to
95
+ # render. For backwards compatibility, we use #response as an alias
96
+ # for #rendered, but it needs to implement #body to avoid
97
+ # `assert_template` raising a `NoMethodError`.
98
+ unless rendered.respond_to?(:body)
99
+ def rendered.body
100
+ self
101
+ end
102
+ end
103
+
104
+ rendered
105
+ end
103
106
 
104
- private
107
+ private
105
108
 
106
- def _default_render_options
107
- if ::Rails::VERSION::STRING >= '3.2'
108
- # pluck the handler, format, and locale out of, eg, posts/index.de.html.haml
109
- template, *components = _default_file_to_render.split('.')
110
- handler, format, locale = *components.reverse
109
+ def _default_render_options
110
+ if ::Rails::VERSION::STRING >= '3.2'
111
+ # pluck the handler, format, and locale out of, eg, posts/index.de.html.haml
112
+ template, *components = _default_file_to_render.split('.')
113
+ handler, format, locale = *components.reverse
111
114
 
112
- render_options = {:template => template}
113
- render_options[:handlers] = [handler] if handler
114
- render_options[:formats] = [format] if format
115
- render_options[:locales] = [locale] if locale
115
+ render_options = { :template => template }
116
+ render_options[:handlers] = [handler] if handler
117
+ render_options[:formats] = [format] if format
118
+ render_options[:locales] = [locale] if locale
116
119
 
117
- render_options
118
- else
119
- {:template => _default_file_to_render}
120
+ render_options
121
+ else
122
+ { :template => _default_file_to_render }
123
+ end
120
124
  end
121
- end
122
125
 
123
- def _path_parts
124
- _default_file_to_render.split("/")
125
- end
126
+ def _path_parts
127
+ _default_file_to_render.split("/")
128
+ end
126
129
 
127
- def _controller_path
128
- _path_parts[0..-2].join("/")
129
- end
130
+ def _controller_path
131
+ _path_parts[0..-2].join("/")
132
+ end
130
133
 
131
- def _inferred_action
132
- _path_parts.last.split(".").first
133
- end
134
+ def _inferred_action
135
+ _path_parts.last.split(".").first
136
+ end
134
137
 
135
- def _include_controller_helpers
136
- helpers = controller._helpers
137
- view.singleton_class.class_exec do
138
- include helpers unless included_modules.include?(helpers)
138
+ def _include_controller_helpers
139
+ helpers = controller._helpers
140
+ view.singleton_class.class_exec do
141
+ include helpers unless included_modules.include?(helpers)
142
+ end
139
143
  end
140
144
  end
141
- end
142
145
 
143
- included do
144
- include ExampleMethods
146
+ included do
147
+ include ExampleMethods
145
148
 
146
- helper(*_default_helpers)
149
+ helper(*_default_helpers)
147
150
 
148
- before do
149
- _include_controller_helpers
150
- if view.lookup_context.respond_to?(:prefixes)
151
- # rails 3.1
152
- view.lookup_context.prefixes << _controller_path
153
- end
151
+ before do
152
+ _include_controller_helpers
153
+ if view.lookup_context.respond_to?(:prefixes)
154
+ # rails 3.1
155
+ view.lookup_context.prefixes << _controller_path
156
+ end
154
157
 
155
- # fixes bug with differing formats
156
- view.lookup_context.view_paths.each(&:clear_cache)
158
+ # fixes bug with differing formats
159
+ view.lookup_context.view_paths.each(&:clear_cache)
157
160
 
158
- controller.controller_path = _controller_path
159
- controller.request.path_parameters[:controller] = _controller_path
160
- controller.request.path_parameters[:action] = _inferred_action unless _inferred_action =~ /^_/
161
- end
161
+ controller.controller_path = _controller_path
162
+ controller.request.path_parameters[:controller] = _controller_path
163
+ controller.request.path_parameters[:action] = _inferred_action unless _inferred_action =~ /^_/
164
+ end
162
165
 
163
- let(:_default_file_to_render) do |example|
164
- example.example_group.top_level_description
166
+ let(:_default_file_to_render) do |example|
167
+ example.example_group.top_level_description
168
+ end
165
169
  end
166
170
  end
167
171
  end
168
172
  end
169
-