[mocha-developer] Nested matchers

James Mead jamesmead44 at gmail.com
Mon Jan 28 19:16:48 EST 2008


On 28/01/2008, Duncan Beevers <duncanbeevers at gmail.com> wrote:
>
> So we cleared this up by making the kind_of matcher to return true
> when tested for equality with an instance of the class it is supposed
> to match on.
>
> Diff is attached, the important bit is here in kind_of.rb:
>
>        def matches?(available_parameters)
>          parameter = available_parameters.shift
> -        parameter.kind_of?(@klass)
> +        self == parameter
>        end
> +
> +      def ==(to_compare)
> +        to_compare.kind_of?(@klass)
> +      end
>
> Feelings?


Well done for finding a fix :-)

The idea of the change to the parameter matchers interface that I mentioned
above [1] is that a matcher can now match against an arbitrary number of
parameters. In particular this is used to allow for constraints on optional
parameters as seen in OptionalParameterMatcherAcceptanceTest, for example:

  def
test_should_pass_if_all_required_and_optional_parameters_match_and_some_optional_parameters_are_supplied
    test_result = run_test do
      mock = mock()
      mock.expects(:method).with(1, 2, optionally(3, 4))
      mock.method(1, 2, 3)
    end
    assert_passed(test_result)
  end

The #==(object) method wasn't really suitable for this, hence the change to
use the #matches?(available_parameters) method.

I hope this makes sense and I hope my previous suggestion of using the new
improved has_entry matcher is satisfactory. The reference [1] is still worth
a read as are the code examples it references.

Thanks.
-- 
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