Skip to content

Commit

Permalink
Fixing bug in the rspec route matcher.
Browse files Browse the repository at this point in the history
* Fixed typo in RouteToMatcher.
* Added the ParameterMatcher's actual and expected parameters to the failure message.
  • Loading branch information
benburkert committed May 26, 2008
1 parent 93b512e commit 79d3514
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/merb-core/test/matchers/route_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,34 @@ def match_parameters(target)
# If parameters is an object, then a new expected hash will be constructed
# with the key :id set to parameters.to_param.
def with(parameters)
@paramter_matcher = ParameterMatcher.new(parameters)
@parameter_matcher = ParameterMatcher.new(parameters)

self
end

# ==== Returns
# String:: The failure message.
def failure_message
"expected the request to route to #{@expected_controller.camel_case}##{@expected_action}, but was #{@target_controller.camel_case}##{@target_action}"
"expected the request to route to #{@expected_controller.camel_case}##{@expected_action}#{expected_parameters_message}, but was #{@target_controller.camel_case}##{@target_action}#{actual_parameters_message}"
end

# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected the request not to route to #{@expected_controller.camel_case}##{@expected_action}, but it did"
"expected the request not to route to #{@expected_controller.camel_case}##{@expected_action}#{expected_parameters_message}, but it did"
end

def expected_parameters_message
" with #{@parameter_matcher.expected.inspect}" if @parameter_matcher
end

def actual_parameters_message
" with #{(@parameter_matcher.actual || {}).inspect}" if @parameter_matcher
end
end

class ParameterMatcher
attr_accessor :expected, :actual

# ==== Parameters
# hash_or_object<Hash, ~to_param>:: The parameters to match.
Expand Down
16 changes: 16 additions & 0 deletions spec/public/test/route_matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ module Merb::Test::Rspec::RouteMatchers
matcher.matches?(:controller => "target_controller", :action => "target_action")
matcher.failure_message.should include("TargetController#target_action")
end

it "should include the expected parameters" do
expected_parameters = {:id => '123', :page => '2'}
matcher = RouteToMatcher.new(TestController, :any_action)
matcher.with(expected_parameters)
matcher.matches?(:controller => "target_controller", :action => "target_action")
matcher.failure_message.should include(expected_parameters.inspect)
end

it "should include the actual paramters" do
expected_parameters, actual_parameters = {:id => '123', :page => '2'}, {:id => '2', :page => '321'}
matcher = RouteToMatcher.new(TestController, :any_action)
matcher.with(expected_parameters)
matcher.matches?(actual_parameters.merge(:controller => "test_controller", :action => "any_action"))
matcher.failure_message.should include(actual_parameters.inspect)
end
end

describe "#negative_failure_message" do
Expand Down

0 comments on commit 79d3514

Please sign in to comment.