Skip to content

route_to Fails with Composing Matchers #1108

Open
@vizjerai

Description

@vizjerai

This worked in RSpec 2.14.1 not sure what changed leading up to RSpec 3.0.2. but it doesn't look like route_to changed much between the two versions.

describe ProductsController do
  describe 'routing' do
    describe 'GET /products/*permalink' do
      it do
        expect(get: '/products/some/path').to route_to 'products#show', permalink: an_instance_of(String)
      end
    end
  end
end

I get an error basically saying 2 hashes are not equal. Most likely the culprit is assert_recognizes so I made a replacement that bypasses it and use recognize_path and do my own assert.

module RSpec::Rails::Matchers
  module RoutingMatchers

    class RouteToWithComposingMatcher < RouteToMatcher
      def matches?(verb_to_path_map)
        @actual = @verb_to_path_map = verb_to_path_map
        # assert_recognizes does not consider ActionController::RoutingError an
        # assertion failure, so we have to capture that and Assertion here.
        match_unless_raises ActiveSupport::TestCase::Assertion, ActionController::RoutingError do
          path, query = *verb_to_path_map.values.first.split('?')
          options = {method: verb_to_path_map.keys.first, extras: Rack::Utils::parse_nested_query(query)}
          route = @scope.routes.recognize_path(path, options)
          @scope.expect(route).to @scope.match(@expected)
        end
      end
    end

    def route_to(*expected)
      RouteToWithComposingMatcher.new(self, *expected)
    end

  end
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions