Sha256: 1d99cbb2d42f970d2c1faf4d3fec6c126061b78e8405e4694f646209f3a93c0e

Contents?: true

Size: 1.11 KB

Versions: 11

Compression:

Stored size: 1.11 KB

Contents

module RSpec
  module Rails
    # Builds paths for view specs using a particular route set.
    class ViewPathBuilder
      def initialize(route_set)
        self.class.send(:include, route_set.url_helpers)
      end

      # Given a hash of parameters, build a view path, if possible.
      # Returns nil if no path can be built from the given params.
      #
      # @example
      #     # path can be built because all required params are present in the hash
      #     view_path_builder = ViewPathBuilder.new(::Rails.application.routes)
      #     view_path_builder.path_for({ :controller => 'posts', :action => 'show', :id => '54' })
      #     # => "/post/54"
      #
      # @example
      #     # path cannot be built because the params are missing a required element (:id)
      #     view_path_builder.path_for({ :controller => 'posts', :action => 'delete' })
      #     # => ActionController::UrlGenerationError: No route matches {:action=>"delete", :controller=>"posts"}
      def path_for(path_params)
        url_for(path_params.merge(:only_path => true))
      rescue => e
        e.message
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rspec-rails-3.9.1 lib/rspec/rails/view_path_builder.rb
rspec-rails-4.0.0.beta3 lib/rspec/rails/view_path_builder.rb
rspec-rails-3.8.3 lib/rspec/rails/view_path_builder.rb
rspec-rails-3.9.0 lib/rspec/rails/view_path_builder.rb
rspec-rails-4.0.0.beta2 lib/rspec/rails/view_path_builder.rb
rspec-rails-3.8.2 lib/rspec/rails/view_path_builder.rb
rspec-rails-3.8.1 lib/rspec/rails/view_path_builder.rb
rspec-rails-3.8.0 lib/rspec/rails/view_path_builder.rb
rspec-rails-3.7.2 lib/rspec/rails/view_path_builder.rb
rspec-rails-3.7.1 lib/rspec/rails/view_path_builder.rb
rspec-rails-3.7.0 lib/rspec/rails/view_path_builder.rb