Class: RspecRailsRouting::Matchers::Routing::HaveNamedRoute

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_rails_routing/matchers/have_named_route.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, name, *args) ⇒ HaveNamedRoute

Returns a new instance of HaveNamedRoute.



9
10
11
12
13
14
15
16
17
18
# File 'lib/rspec_rails_routing/matchers/have_named_route.rb', line 9

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

Instance Method Details

#descriptionObject



20
21
22
# File 'lib/rspec_rails_routing/matchers/have_named_route.rb', line 20

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

#example_callObject



41
42
43
44
45
46
47
48
# File 'lib/rspec_rails_routing/matchers/have_named_route.rb', line 41

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

  call
end

#failure_message_for_shouldObject



33
34
35
# File 'lib/rspec_rails_routing/matchers/have_named_route.rb', line 33

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

#failure_message_for_should_notObject



37
38
39
# File 'lib/rspec_rails_routing/matchers/have_named_route.rb', line 37

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

#format_args(args) ⇒ Object



50
51
52
53
54
# File 'lib/rspec_rails_routing/matchers/have_named_route.rb', line 50

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

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# File 'lib/rspec_rails_routing/matchers/have_named_route.rb', line 24

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