Class: Merb::Test::Rspec::RouteMatchers::ParameterMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-core/test/matchers/route_matchers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash_or_object) ⇒ ParameterMatcher

Parameters

hash_or_object<Hash, ~to_param>

The parameters to match.

Alternatives

If hash_or_object is an object, then a new expected hash will be constructed with the key :id set to hash_or_object.to_param.


86
87
88
89
90
91
92
# File 'lib/merb-core/test/matchers/route_matchers.rb', line 86

def initialize(hash_or_object)
  @expected = {}
  case hash_or_object
  when Hash then @expected = hash_or_object
  else @expected[:id] = hash_or_object.to_param
  end
end

Instance Attribute Details

#actualObject

Returns the value of attribute actual


78
79
80
# File 'lib/merb-core/test/matchers/route_matchers.rb', line 78

def actual
  @actual
end

#expectedObject

Returns the value of attribute expected


78
79
80
# File 'lib/merb-core/test/matchers/route_matchers.rb', line 78

def expected
  @expected
end

Instance Method Details

#failure_messageObject

Returns

String

The failure message.


108
109
110
# File 'lib/merb-core/test/matchers/route_matchers.rb', line 108

def failure_message
  "expected the route to contain parameters #{@expected.inspect}, but instead contained #{@actual.inspect}"
end

#matches?(parameter_hash) ⇒ Boolean

Parameters

parameter_hash<Hash>

The route parameters to match.

Returns

Boolean

True if the route parameters match the expected ones.

Returns:

  • (Boolean)

99
100
101
102
103
104
# File 'lib/merb-core/test/matchers/route_matchers.rb', line 99

def matches?(parameter_hash)
  @actual = parameter_hash.dup.except(:controller, :action)

  return @actual.empty? if @expected.empty?
  @expected.all? {|(k, v)| @actual.has_key?(k) && @actual[k] == v}
end

#negative_failure_messageObject

Returns

String

The failure message to be displayed in negative matches.


114
115
116
# File 'lib/merb-core/test/matchers/route_matchers.rb', line 114

def negative_failure_message
  "expected the route not to contain parameters #{@expected.inspect}, but it did"
end