method

hash_including

rspec latest stable - Class: Spec::Mocks::ArgumentConstraintMatchers

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (1.1.4) is shown here.

hash_including(expected={})
public

No documentation available.

# File lib/spec/mocks/argument_constraint_matchers.rb, line 22
      def hash_including(expected={})
        HashIncludingConstraint.new(expected)
      end

1Note

Testing an options hash receives certain parameters

Olly ยท Aug 14, 200813 thanks

This method is very useful for testing methods that use the ruby idiom of accepting a hash with configurable options.

class Example
def self.find(options = {})
  ...
end
end

We can use hash_including to ensure that certain options are passed in when mocking it.

Example.should_receive(:find).with(hash_including(:conditions => 'some conditions'))

Example.find(:conditions => 'some_conditions', :order => 1)
# => Passes expectation

Example.find(:order => 1)
# => Fails expectation

This can also be used to great effect with the anything matcher. For example: hash_including(:key => anything)

hash_including(anything => 'value')