#501 √ resolved
Pirkka Hartikainen

hash_including(:key => anything) fails

Reported by Pirkka Hartikainen | August 20th, 2008 @ 03:49 PM | in 1.1.5

hash_including works fine with "anything" as a key, as long as the key is an integer. if it's a string, something else happens.

here's the code:


require 'rubygems'
require 'spec'

class Server
  def self.do_something(options)
    puts "doing something with options"
  end
end

class Client
  def self.do_something(argument)
    Server.do_something(:some_key => argument)
  end
end

describe Client do
  it "should call server with a numeric argument passed inside a hash with key :some_key " do
    Server.should_receive(:do_something).with(hash_including(:some_key => anything))
    Client.do_something(234)
  end
  it "should call server with a string argument passed inside a hash with key :some_key " do
    Server.should_receive(:do_something).with(hash_including(:some_key => anything))
    Client.do_something("234")
  end
end

here's the output:


xwing:code$ spec -v
RSpec-1.1.4 (build 20080526202855) - BDD for Ruby
http://rspec.rubyforge.org/
xwing:code$ spec anything.rb
.F

1)
Spec::Mocks::MockExpectationError in 'Client should call server with a string argument passed inside a hash with key :some_key '
Mock 'Class' expected :do_something with (hash_including(:some_key=>#<Spec::Mocks::AnyArgConstraint:0x561df0>)) but received it with ({:some_key=>"234"})
./anything.rb:12:in `do_something'
./anything.rb:23:

Finished in 0.018847 seconds

2 examples, 1 failure

expected that the second example would have passed.

Comments and changes to this ticket

  • Pirkka Hartikainen

    Pirkka Hartikainen August 21st, 2008 @ 01:17 PM

    It looks like this re-ordering the == comparison in HashIncludingConstraint helps:

    
    argument_expectation.rb:
    
    class HashIncludingConstraint
          def initialize(expected)
            @expected = expected
          end
    
          def ==(actual)
            @expected.each do | key, value |
              # check key for case that value evaluates to nil
              #
              # return false unless actual.has_key?(key) && actual[key] == value
              return false unless actual.has_key?(key) && value == actual[key]
              # ^ the == operator of AnyArgumentConstraint always returns true
            end
            true
          rescue NoMethodError => ex
            return false
          end
    
          def matches?(value)
            self == value
          end
    
          def description
            "hash_including(#{@expected.inspect.sub(/^\{/,"").sub(/\}$/,"")})"
          end
    
        end
    
  • David Chelimsky

    David Chelimsky September 28th, 2008 @ 06:35 PM

    (from [fa494fc9bddfac0fbd2a89ca202e88d7904df1b7]) add failing example (pending) for [#501] http://github.com/dchelimsky/rsp...

  • David Chelimsky

    David Chelimsky September 28th, 2008 @ 06:35 PM

    • → Assigned user changed from “” to “David Chelimsky”
    • → State changed from “new” to “open”
  • David Chelimsky

    David Chelimsky September 28th, 2008 @ 10:20 PM

    • → State changed from “open” to “resolved”
    • → Milestone changed from “No-Milestone-Assigned” to “1.1.5”

    (from [a5baf9f47e17c121f9e1de13e69807283465a7f9]) Support argument constraints as values in the hash_including contstraint. Buttons up [#501 state:resolved milestone:"1.1.5"] http://github.com/dchelimsky/rsp...

  • David Chelimsky

    David Chelimsky September 28th, 2008 @ 10:21 PM

    Pirkka,

    Thanks for the failing examples and the hint for the fix. Next time you should patch it yourself so we can get your name in the commit logs!

    Cheers, David

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Behaviour Driven Development for Ruby.

Shared Ticket Bins

People watching this ticket