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
@@ -1,109 +1,123 @@
1
- module RSpec::Rails::Matchers
2
- module RoutingMatchers
3
- extend RSpec::Matchers::DSL
1
+ module RSpec
2
+ module Rails
3
+ module Matchers
4
+ # Matchers to help with specs for routing code.
5
+ module RoutingMatchers
6
+ extend RSpec::Matchers::DSL
4
7
 
5
- class RouteToMatcher < RSpec::Matchers::BuiltIn::BaseMatcher
8
+ # @private
9
+ class RouteToMatcher < RSpec::Rails::Matchers::BaseMatcher
10
+ def initialize(scope, *expected)
11
+ @scope = scope
12
+ @expected = expected[1] || {}
13
+ if Hash === expected[0]
14
+ @expected.merge!(expected[0])
15
+ else
16
+ controller, action = expected[0].split('#')
17
+ @expected.merge!(controller: controller, action: action)
18
+ end
19
+ end
6
20
 
7
- def initialize(scope, *expected)
8
- @scope = scope
9
- @expected = expected[1] || {}
10
- if Hash === expected[0]
11
- @expected.merge!(expected[0])
12
- else
13
- controller, action = expected[0].split('#')
14
- @expected.merge!(:controller => controller, :action => action)
15
- end
16
- end
17
-
18
- # @api private
19
- def matches?(verb_to_path_map)
20
- @actual = @verb_to_path_map = verb_to_path_map
21
- # assert_recognizes does not consider ActionController::RoutingError an
22
- # assertion failure, so we have to capture that and Assertion here.
23
- match_unless_raises ActiveSupport::TestCase::Assertion, ActionController::RoutingError do
24
- path, query = *verb_to_path_map.values.first.split('?')
25
- @scope.assert_recognizes(
26
- @expected,
27
- {:method => verb_to_path_map.keys.first, :path => path},
28
- Rack::Utils::parse_query(query)
29
- )
30
- end
31
- end
21
+ def matches?(verb_to_path_map)
22
+ @actual = verb_to_path_map
23
+ # assert_recognizes does not consider ActionController::RoutingError an
24
+ # assertion failure, so we have to capture that and Assertion here.
25
+ match_unless_raises ActiveSupport::TestCase::Assertion, ActionController::RoutingError do
26
+ path, query = *verb_to_path_map.values.first.split('?')
27
+ @scope.assert_recognizes(
28
+ @expected,
29
+ {method: verb_to_path_map.keys.first, path: path},
30
+ Rack::Utils.parse_nested_query(query)
31
+ )
32
+ end
33
+ end
32
34
 
33
- # @api private
34
- def failure_message_for_should
35
- rescued_exception.message
36
- end
35
+ def failure_message
36
+ rescued_exception.message
37
+ end
37
38
 
38
- def failure_message_for_should_not
39
- "expected #{@actual.inspect} not to route to #{@expected.inspect}"
40
- end
39
+ def failure_message_when_negated
40
+ "expected #{@actual.inspect} not to route to #{@expected.inspect}"
41
+ end
41
42
 
42
- def description
43
- "route #{@actual.inspect} to #{@expected.inspect}"
44
- end
45
- end
43
+ def description
44
+ "route #{@actual.inspect} to #{@expected.inspect}"
45
+ end
46
+ end
46
47
 
47
- # Delegates to `assert_recognizes`. Supports short-hand controller/action
48
- # declarations (e.g. `"controller#action"`).
49
- #
50
- # @example
51
- #
52
- # { :get => "/things/special" }.should route_to(
53
- # :controller => "things",
54
- # :action => "special"
55
- # )
56
- #
57
- # { :get => "/things/special" }.should route_to("things#special")
58
- #
59
- # @see http://api.rubyonrails.org/classes/ActionDispatch/Assertions/RoutingAssertions.html#method-i-assert_recognizes
60
- def route_to(*expected)
61
- RouteToMatcher.new(self, *expected)
62
- end
48
+ # Delegates to `assert_recognizes`. Supports short-hand controller/action
49
+ # declarations (e.g. `"controller#action"`).
50
+ #
51
+ # @example
52
+ #
53
+ # expect(get: "/things/special").to route_to(
54
+ # controller: "things",
55
+ # action: "special"
56
+ # )
57
+ #
58
+ # expect(get: "/things/special").to route_to("things#special")
59
+ #
60
+ # @see https://api.rubyonrails.org/classes/ActionDispatch/Assertions/RoutingAssertions.html#method-i-assert_recognizes
61
+ def route_to(*expected)
62
+ RouteToMatcher.new(self, *expected)
63
+ end
63
64
 
64
- class BeRoutableMatcher < RSpec::Matchers::BuiltIn::BaseMatcher
65
+ # @private
66
+ class BeRoutableMatcher < RSpec::Rails::Matchers::BaseMatcher
67
+ def initialize(scope)
68
+ @scope = scope
69
+ end
65
70
 
66
- def initialize(scope)
67
- @scope = scope
68
- end
71
+ def matches?(path)
72
+ @actual = path
73
+ match_unless_raises ActionController::RoutingError do
74
+ @routing_options = @scope.routes.recognize_path(
75
+ path.values.first, method: path.keys.first
76
+ )
77
+ end
78
+ end
69
79
 
70
- # @api private
71
- def matches?(path)
72
- @actual = path
73
- match_unless_raises ActionController::RoutingError do
74
- @routing_options = @scope.routes.recognize_path(
75
- path.values.first, :method => path.keys.first
76
- )
77
- end
78
- end
80
+ def failure_message
81
+ "expected #{@actual.inspect} to be routable"
82
+ end
79
83
 
80
- def failure_message_for_should
81
- "expected #{@actual.inspect} to be routable"
82
- end
84
+ def failure_message_when_negated
85
+ "expected #{@actual.inspect} not to be routable, but it routes to #{@routing_options.inspect}"
86
+ end
83
87
 
84
- def failure_message_for_should_not
85
- "expected #{@actual.inspect} not to be routable, but it routes to #{@routing_options.inspect}"
86
- end
87
- end
88
+ def description
89
+ "be routable"
90
+ end
91
+ end
88
92
 
89
- # Passes if the route expression is recognized by the Rails router based on
90
- # the declarations in `config/routes.rb`. Delegates to
91
- # `RouteSet#recognize_path`.
92
- #
93
- # @example
94
- #
95
- # You can use route helpers provided by rspec-rails.
96
- # {:get => "/a/path"}.should be_routable
97
- # {:post => "/another/path"}.should be_routable
98
- # {:put => "/yet/another/path"}.should_not be_routable
99
- def be_routable
100
- BeRoutableMatcher.new(self)
101
- end
93
+ # Passes if the route expression is recognized by the Rails router based on
94
+ # the declarations in `config/routes.rb`. Delegates to
95
+ # `RouteSet#recognize_path`.
96
+ #
97
+ # @example You can use route helpers provided by rspec-rails.
98
+ # expect(get: "/a/path").to be_routable
99
+ # expect(post: "/another/path").to be_routable
100
+ # expect(put: "/yet/another/path").to be_routable
101
+ def be_routable
102
+ BeRoutableMatcher.new(self)
103
+ end
102
104
 
103
- module RouteHelpers
104
- %w(get post put patch delete options head).each do |method|
105
- define_method method do |path|
106
- { method.to_sym => path }
105
+ # Helpers for matching different route types.
106
+ module RouteHelpers
107
+ # @!method get
108
+ # @!method post
109
+ # @!method put
110
+ # @!method patch
111
+ # @!method delete
112
+ # @!method options
113
+ # @!method head
114
+ #
115
+ # Shorthand method for matching this type of route.
116
+ %w[get post put patch delete options head].each do |method|
117
+ define_method method do |path|
118
+ {method.to_sym => path}
119
+ end
120
+ end
107
121
  end
108
122
  end
109
123
  end
@@ -1,27 +1,35 @@
1
- require 'rspec/core/deprecation'
1
+ require 'rspec/core/warnings'
2
2
  require 'rspec/expectations'
3
+ require 'rspec/rails/feature_check'
3
4
 
4
- module RSpec::Rails
5
- module Matchers
6
- end
7
- end
8
-
9
- begin
10
- require 'test/unit/assertionfailederror'
11
- rescue LoadError
12
- module Test
13
- module Unit
14
- class AssertionFailedError < StandardError
15
- end
5
+ module RSpec
6
+ module Rails
7
+ # @api public
8
+ # Container module for Rails specific matchers.
9
+ module Matchers
16
10
  end
17
11
  end
18
12
  end
19
13
 
14
+ require 'rspec/rails/matchers/base_matcher'
20
15
  require 'rspec/rails/matchers/have_rendered'
21
16
  require 'rspec/rails/matchers/redirect_to'
22
17
  require 'rspec/rails/matchers/routing_matchers'
23
18
  require 'rspec/rails/matchers/be_new_record'
24
19
  require 'rspec/rails/matchers/be_a_new'
25
- require 'rspec/rails/matchers/have_extension'
26
20
  require 'rspec/rails/matchers/relation_match_array'
27
21
  require 'rspec/rails/matchers/be_valid'
22
+ require 'rspec/rails/matchers/have_http_status'
23
+
24
+ if RSpec::Rails::FeatureCheck.has_active_job?
25
+ require 'rspec/rails/matchers/active_job'
26
+ require 'rspec/rails/matchers/have_enqueued_mail'
27
+ end
28
+
29
+ if RSpec::Rails::FeatureCheck.has_action_cable_testing?
30
+ require 'rspec/rails/matchers/action_cable'
31
+ end
32
+
33
+ if RSpec::Rails::FeatureCheck.has_action_mailbox?
34
+ require 'rspec/rails/matchers/action_mailbox'
35
+ end
@@ -3,26 +3,26 @@ if default = Rake.application.instance_variable_get('@tasks')['default']
3
3
  default.prerequisites.delete('test')
4
4
  end
5
5
 
6
- task :default => :spec
6
+ task default: :spec
7
7
 
8
- task :stats => "spec:statsetup"
8
+ task stats: "spec:statsetup"
9
9
 
10
10
  desc "Run all specs in spec directory (excluding plugin specs)"
11
- RSpec::Core::RakeTask.new(:spec => "spec:prepare")
11
+ RSpec::Core::RakeTask.new(spec: "spec:prepare")
12
12
 
13
13
  namespace :spec do
14
14
  types = begin
15
- dirs = Dir['./spec/**/*_spec.rb'].
16
- map { |f| f.sub(/^\.\/(spec\/\w+)\/.*/, '\\1') }.
17
- uniq.
18
- select { |f| File.directory?(f) }
15
+ dirs = Dir['./spec/**/*_spec.rb']
16
+ .map { |f| f.sub(/^\.\/(spec\/\w+)\/.*/, '\\1') }
17
+ .uniq
18
+ .select { |f| File.directory?(f) }
19
19
  Hash[dirs.map { |d| [d.split('/').last, d] }]
20
20
  end
21
21
 
22
22
  task :prepare do
23
23
  ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'
24
24
  if Rails.configuration.generators.options[:rails][:orm] == :active_record
25
- if ::Rails::VERSION::STRING.to_f < 4.1
25
+ if Rake::Task.task_defined?("test:prepare")
26
26
  Rake::Task["test:prepare"].invoke
27
27
  end
28
28
  end
@@ -35,16 +35,6 @@ namespace :spec do
35
35
  end
36
36
  end
37
37
 
38
- # RCov task only enabled for Ruby 1.8
39
- if RUBY_VERSION < '1.9'
40
- desc "Run all specs with rcov"
41
- RSpec::Core::RakeTask.new(:rcov => "spec:prepare") do |t|
42
- t.rcov = true
43
- t.pattern = "./spec/**/*_spec.rb"
44
- t.rcov_opts = '--exclude /gems/,/Library/,/usr/,lib/tasks,.bundle,config,/lib/rspec/,/lib/rspec-,spec'
45
- end
46
- end
47
-
48
38
  task :statsetup do
49
39
  require 'rails/code_statistics'
50
40
  types.each do |type, dir|
@@ -11,23 +11,24 @@ end
11
11
  if defined?(Capybara)
12
12
  RSpec.configure do |c|
13
13
  if defined?(Capybara::DSL)
14
- c.include Capybara::DSL, :type => :controller
15
- c.include Capybara::DSL, :type => :feature
14
+ c.include Capybara::DSL, type: :feature
15
+ if defined?(ActionPack) && ActionPack::VERSION::STRING >= "5.1"
16
+ c.include Capybara::DSL, type: :system
17
+ end
16
18
  end
17
19
 
18
20
  if defined?(Capybara::RSpecMatchers)
19
- c.include Capybara::RSpecMatchers, :type => :view
20
- c.include Capybara::RSpecMatchers, :type => :helper
21
- c.include Capybara::RSpecMatchers, :type => :mailer
22
- c.include Capybara::RSpecMatchers, :type => :controller
23
- c.include Capybara::RSpecMatchers, :example_group => {
24
- :file_path => c.escaped_path(%w[spec features])
25
- }
21
+ c.include Capybara::RSpecMatchers, type: :view
22
+ c.include Capybara::RSpecMatchers, type: :helper
23
+ c.include Capybara::RSpecMatchers, type: :mailer
24
+ c.include Capybara::RSpecMatchers, type: :controller
25
+ c.include Capybara::RSpecMatchers, type: :feature
26
+ c.include Capybara::RSpecMatchers, type: :system
26
27
  end
27
28
 
28
29
  unless defined?(Capybara::RSpecMatchers) || defined?(Capybara::DSL)
29
- c.include Capybara, :type => :request
30
- c.include Capybara, :type => :controller
30
+ c.include Capybara, type: :request
31
+ c.include Capybara, type: :controller
31
32
  end
32
33
  end
33
34
  end
@@ -1,7 +1,9 @@
1
1
  module RSpec
2
2
  module Rails
3
+ # Version information for RSpec Rails.
3
4
  module Version
4
- STRING = '2.14.2'
5
+ # Current version of RSpec Rails, in semantic versioning format.
6
+ STRING = '4.1.2'
5
7
  end
6
8
  end
7
9
  end
@@ -1,5 +1,6 @@
1
1
  module RSpec
2
2
  module Rails
3
+ # Helpers for making instance variables available to views.
3
4
  module ViewAssigns
4
5
  # Assigns a value to an instance variable in the scope of the
5
6
  # view being rendered.
@@ -11,35 +12,34 @@ module RSpec
11
12
  _encapsulated_assigns[key] = value
12
13
  end
13
14
 
15
+ # Compat-shim for AbstractController::Rendering#view_assigns
16
+ #
17
+ # _assigns was deprecated in favor of view_assigns after
18
+ # Rails-3.0.0 was released. Since we are not able to predict when
19
+ # the _assigns/view_assigns patch will be released (I thought it
20
+ # would have been in 3.0.1, but 3.0.1 bypassed this change for a
21
+ # security fix), this bit ensures that we do the right thing without
22
+ # knowing anything about the Rails version we are dealing with.
23
+ #
24
+ # Once that change _is_ released, this can be changed to something
25
+ # that checks for the Rails version when the module is being
26
+ # interpreted, as it was before commit dd0095.
14
27
  def view_assigns
15
- begin
16
- # TODO: _assigns was deprecated in favor of view_assigns after
17
- # Rails-3.0.0 was released. Since we are not able to predict when
18
- # the _assigns/view_assigns patch will be released (I thought it
19
- # would have been in 3.0.1, but 3.0.1 bypassed this change for a
20
- # security fix), this bit ensures that we do the right thing without
21
- # knowing anything about the Rails version we are dealing with.
22
- #
23
- # Once that change _is_ released, this can be changed to something
24
- # that checks for the Rails version when the module is being
25
- # interpreted, as it was before commit dd0095.
26
- super.merge(_encapsulated_assigns)
27
- rescue
28
- _assigns
29
- end
28
+ super.merge(_encapsulated_assigns)
29
+ rescue
30
+ _assigns
30
31
  end
31
32
 
32
- # @api private
33
+ # @private
33
34
  def _assigns
34
35
  super.merge(_encapsulated_assigns)
35
36
  end
36
37
 
37
- private
38
+ private
38
39
 
39
40
  def _encapsulated_assigns
40
41
  @_encapsulated_assigns ||= {}
41
42
  end
42
-
43
43
  end
44
44
  end
45
45
  end
@@ -0,0 +1,29 @@
1
+ module RSpec
2
+ module Rails
3
+ # Builds paths for view specs using a particular route set.
4
+ class ViewPathBuilder
5
+ def initialize(route_set)
6
+ self.class.send(:include, route_set.url_helpers)
7
+ end
8
+
9
+ # Given a hash of parameters, build a view path, if possible.
10
+ # Returns nil if no path can be built from the given params.
11
+ #
12
+ # @example
13
+ # # path can be built because all required params are present in the hash
14
+ # view_path_builder = ViewPathBuilder.new(::Rails.application.routes)
15
+ # view_path_builder.path_for({ :controller => 'posts', :action => 'show', :id => '54' })
16
+ # # => "/post/54"
17
+ #
18
+ # @example
19
+ # # path cannot be built because the params are missing a required element (:id)
20
+ # view_path_builder.path_for({ :controller => 'posts', :action => 'delete' })
21
+ # # => ActionController::UrlGenerationError: No route matches {:action=>"delete", :controller=>"posts"}
22
+ def path_for(path_params)
23
+ url_for(path_params.merge(only_path: true))
24
+ rescue => e
25
+ e.message
26
+ end
27
+ end
28
+ end
29
+ end