Sha256: 5ad1257e4407d705b87af2235e997c20f76a9af5cac985fdad2bbf515233291e

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require 'rspec/expectations'

module RspecRailsRouting
  module Matchers
    module Routing

      class HaveNamedRoute
        def initialize(context, name, *args)
          @context = context
          @name = name
          @path = "#{name}_path"
          @args = args
          if ! args.last
            raise ArgumentError, 'The last argument must be the expected uri'
          end
          @expected_uri = args.pop
        end

        def description
          "have a route named #{@name}, where e.g. #{example_call} == #{@expected_uri}"
        end

        def matches?(subject)
          begin
            @actual_uri = @context.send( "#{@name}_path", *@args )
            @actual_uri == @expected_uri
          rescue NoMethodError
            false
          end
        end

        def failure_message_for_should
          "expected #{example_call} to equal #{@expected_uri}, but got #{@actual_uri}"
        end

        def failure_message_for_should_not
          "expected #{example_call} to not equal #{@expected_uri}, but it did"
        end

        def example_call
          call = "#{@name}_path"
          if ! @args.empty?
            call << "(#{format_args( @args )})"
          end

          call
        end

        def format_args( args )
          @args.map do |a|
            a.is_a?( Hash ) ? a.inspect : a.to_s
          end.join( ', ' )
        end

      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-rails-routing-1.0.0 lib/rspec_rails_routing/matchers/have_named_route.rb