[mocha-developer] Nested matchers

James Mead jamesmead44 at gmail.com
Mon Jan 28 18:59:02 EST 2008


On 28/01/2008, Duncan Beevers <duncanbeevers at gmail.com> wrote:
>
> We're encountering a failure with Mocha 0.5.6.
>
> We had this expectation:
>
> game_version.expects(:attributes=).with(:game_file =>
> kind_of(GameFile), :game_id => @game.id)
>
> This expectation was passing with 0.5.5, but fails with 0.5.6.
>
> I added this test to parameter_matcher_acceptance_test.rb, which
> passes in 0.5.5 and fails in 0.5.6
>
>   def test_should_match_nested_parameters
>     test_result = run_test do
>       mock = mock()
>       mock.expects(:method).with(:literal_key => kind_of(Integer))
>       mock.method(:literal_key => 1)
>     end
>     assert_passed(test_result)
>   end
>
> Any insight?
>

Sorry. I previously explained this change when it was made in trunk [1]. I
had intended to warn people again when the change was released, but I
released v0.5.6 in a bit of a hurry to provide Ruby v1.9 compatibility and
the change slipped out.

I hadn't realised people were using the matchers in this way. I think a
better solution is to make the HasEntry parameter matcher (and friends)
convert its key & value to parameter matchers using #to_matcher and to
compare these using #matches? In this way you would be able to do something
like...

  def test_should_match_nested_parameters
   test_result = run_test do
     mock = mock()
     mock.expects(:method).with(has_entry(:literal_key => kind_of(Integer)))
     mock.method(:literal_key => 1)
   end
   assert_passed(test_result)
  end

I've checked in some changes in revision 245 of trunk if you want to try
them out. Let me know if I've misunderstood or made a mistake.
-- 
James.
http://blog.floehopper.org
http://tumble.floehopper.org

[1] http://rubyforge.org/pipermail/mocha-developer/2007-November/000482.html


More information about the mocha-developer mailing list