Module: Riot::Gear::AssertsJson

Defined in:
lib/riot/gear/context/asserts_json.rb

Instance Method Summary collapse

Instance Method Details

#asserts_json(json_string, &handler) ⇒ Riot::Assertion

Generates an assertion that based on the value returned from passing the JSON path to the json_path helper. If a handler block is provided, that block will be called with the value and the response from the block will be used as the actual in the assertion test.

 context "testing a hash" do
   setup do
     {"a" => {"b" => {"c" => {"d" => "foo"}}}}
   end

   asserts_json("a.b.c.d").equals("foo")
   asserts_json("a['b'].c['d']").equals("foo")

   asserts_json("a.b") do |value|
     value["c"]
   end.equals({"d" => "foo"})
end

This is useful for testing actual JSON responses from some service that are converted to a hash by HTTParty.

Parameters:

  • json_string (String)

    a JSON looking path

  • &handler (lambda)

    an optional block for filtering the actual value

Returns:

  • (Riot::Assertion)

    an assertion block that macros can be applied to



30
31
32
33
34
35
# File 'lib/riot/gear/context/asserts_json.rb', line 30

def asserts_json(json_string, &handler)
  asserts("value from body as json:#{json_string}") do
    value = json_path(response, json_string)
    handler ? handler.call(value) : value
  end
end

#denies_json(json_string, &handler) ⇒ Riot::Assertion

Generates an denies that based on the value returned from passing the JSON path to the json_path helper. If a handler block is provided, that block will be called with the value and the response from the block will be used as the actual in the denies test.

 context "testing a hash" do
   setup do
     {"a" => {"b" => {"c" => {"d" => "foo"}}}}
   end

   denies_json("a.b.c.d").equals("bar")
   denies_json("a['b'].c['d']").equals("bar")

   denies_json("a.b") do |value|
     value["c"]
   end.equals({"d" => "bar"})
end

This is useful for testing actual JSON responses from some service that are converted to a hash by HTTParty.

Parameters:

  • json_string (String)

    a JSON looking path

  • &handler (lambda)

    an optional block for filtering the actual value

Returns:

  • (Riot::Assertion)

    an denies block that macros can be applied to



60
61
62
63
64
65
# File 'lib/riot/gear/context/asserts_json.rb', line 60

def denies_json(json_string, &handler)
  denies("value from body as json:#{json_string}") do
    value = json_path(response, json_string)
    handler ? handler.call(value) : value
  end
end