Skip to content
This repository

Provide json_equivalent parameter matcher (similar to yaml_equivalent) #36

Closed
wants to merge 2 commits into from

3 participants

Sebastian Röbke Andy Lindeman James Mead
Sebastian Röbke

In tests I often have the need of expecting parameters in JSON format. That's why I added a json_equivalent parameter matcher, similar to the existing yaml_equivalent.

Please note that this introduces a gem dependency on the json gem. Using a different JSON gem would certainly be possible.

Andy Lindeman

Might consider multi_json?

Sebastian Röbke

Yes, good idea! Will do.

Sebastian Röbke boosty Use multi_json instead of concrete json lib
Also get rid of unnecessary json encode/decode 
sequence.
84a5c68
James Mead
Owner

Thanks for the pull request. Sorry not to have replied to this sooner. My immediate reaction was that I don't really want to introduce a dependency on another gem just for this matcher. The YAML matcher is different in that it only depends on the standard library in Ruby 1.8.7. But I notice that JSON is available in the Ruby 1.9.2 standard library, so I'll give this some more thought. One solution might be not to explicitly load the JSON matcher by default, so a developer has to explicitly load it. What do you think?

Sebastian Röbke

Thanks for the feedback!

I think you are right, adding a gem dependency just for this matcher seems a bit too much.

A helpful message when using the matcher without the gem being installed would be my idea. Will try to come with some code in the next days.

James Mead
Owner

In the absence of a version which doesn't introduce a dependency on a JSON gem, I'm closing this. Thanks.

James Mead floehopper referenced this pull request from a commit in mrsimo/mocha
Albert Llop mrsimo Test for prepended methods 8ff901d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 2 authors.

Sep 21, 2011
Sebastian Röbke boosty Provide json_equivalent parameter matcher (similar to yaml_equivalent) f2d6f80
Oct 22, 2011
Sebastian Röbke boosty Use multi_json instead of concrete json lib
Also get rid of unnecessary json encode/decode 
sequence.
84a5c68
This page is out of date. Refresh to see the latest.
43 lib/mocha/parameter_matchers/json_equivalent.rb
... ... @@ -0,0 +1,43 @@
  1 +require 'mocha/parameter_matchers/base'
  2 +require 'multi_json'
  3 +
  4 +module Mocha
  5 +
  6 + module ParameterMatchers
  7 +
  8 + # :call-seq: json_equivalent(object) -> parameter_matcher
  9 + #
  10 + # Matches any JSON that represents the specified +object+
  11 + # object = mock()
  12 + # object.expects(:method_1).with(json_equivalent({"foo" => "bar"}))
  13 + # object.method_1("{\"foo\":\"bar\"}")
  14 + # # no error raised
  15 + #
  16 + # object = mock()
  17 + # object.expects(:method_1).with(json_equivalent({"foo" => "bar"}))
  18 + # object.method_1("{\"foo\":\"BAD\"}")
  19 + # # error raised, because method_1 was not called with JSON representing the specified Hash
  20 + def json_equivalent(object)
  21 + JsonEquivalent.new(object)
  22 + end
  23 +
  24 + class JsonEquivalent < Base # :nodoc:
  25 +
  26 + def initialize(object)
  27 + @object = object
  28 + end
  29 +
  30 + def matches?(available_parameters)
  31 + parameter = available_parameters.shift
  32 + @object == MultiJson.decode(parameter)
  33 + end
  34 +
  35 + def mocha_inspect
  36 + "json_equivalent(#{@object.mocha_inspect})"
  37 + end
  38 +
  39 + end
  40 +
  41 + end
  42 +
  43 +end
1  mocha.gemspec
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22 22 s.summary = "Mocking and stubbing library"
23 23
24 24 s.add_dependency("metaclass", "~> 0.0.1")
  25 + s.add_dependency("multi_json", "~> 1.0.1")
25 26 if s.respond_to? :specification_version then
26 27 current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
27 28 s.specification_version = 3
25 test/unit/parameter_matchers/json_equivalent_test.rb
... ... @@ -0,0 +1,25 @@
  1 +require File.expand_path('../../../test_helper', __FILE__)
  2 +
  3 +require 'mocha/parameter_matchers/json_equivalent'
  4 +require 'mocha/inspect'
  5 +
  6 +class JsonEquivalentTest < Test::Unit::TestCase
  7 +
  8 + include Mocha::ParameterMatchers
  9 +
  10 + def test_should_match_parameter_matching_json_representation_of_object
  11 + matcher = json_equivalent({"foo" => "bar"})
  12 + assert matcher.matches?(["{\"foo\":\"bar\"}"])
  13 + end
  14 +
  15 + def test_should_not_match_parameter_not_matching_json_representation_of_object
  16 + matcher = json_equivalent({"foo" => "bar"})
  17 + assert !matcher.matches?(["{\"foo\":\"BAD\"}"])
  18 + end
  19 +
  20 + def test_should_describe_matcher
  21 + matcher = json_equivalent({"foo" => "bar"})
  22 + assert_equal "json_equivalent({'foo' => 'bar'})", matcher.mocha_inspect
  23 + end
  24 +
  25 +end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.