Skip to content
deepak edited this page Mar 1, 2015 · 7 revisions

Response Body

Stubbing simple response body is explained here. This page explains stubbing of

Array Field

In the below example response body has a field that is of type array

Important: mention just a single value of array in the config.

#cost/gross_cost.yml
request:
  url: gross
response:
  body: '{
  "costs":[10]
  }'
GrossCost.new.build 
GrossCost.new.has_cost.build 
#=> builds localhost:3030/cost/gross which returns 
#{"cost":[10]}

Note: In above example, both the statements results the same response. By default, array fields assigns values as mentioned in config. And the first modifier "has_[singular_of_field_name]" does the same.

Using "has_[field_name]" method which takes array directly as parameter

GrossCost.new.has_costs([30,20]).build 
#=> which returns {"costs":[30,20]}

Using "has_[singular_of_field_name]" consecutively add multiple values to array.

GrossCost.new.has_cost.has_cost(20).build 
#=> which returns {"costs":[10,20]}
GrossCost.new.has_cost(30).has_cost(20).build 
#=> which returns {"costs":[30,20]}

Nested Array Field

In the below example response body has a field that is of type array of object. It works the same as the above, just that it has extra modifier for the nested objects.

Important: mention just a single value of array in the config.

#cost/gross_cost.yml
request:
  url: gross
response:
  body: '{
  "costs":[{"article":"soap","value":1}]}'
GrossCost.new.build 
GrossCost.new.has_cost.build 
#=> builds localhost:3030/cost/gross which returns 
#{"costs":[{"article":"soap","value":1}]}

Note: In above example, both the statements results the same response. By default, array fields assigns values as mentioned in config. And the first modifier "has_[singular_of_field_name]" does the same.

Using "has_[filed_name]" method which takes array hash-map as parameter, to modify all or partial list of nested fields.

GrossCost.new.
  has_costs([{:value=>"10"},{:article=>"shampoo",:value=>4}].build 
#=> which returns 
#{"costs":[
# {"article":"soap","value":10},
# {"article":"shampoo","value":4}
#]}

Using "has_[singular_of_field_name]" method consecutively add multiple values to field.

GrossCost.new.
  has_cost(:value=>"10").
  has_cost({:article=>"shampoo",:value=>4}).build 
#=> which returns 
#{"costs":[
# {"article":"soap","value":10},
# {"article":"shampoo","value":4}
#]}

Using "with_[nested_field]" method assign value to the nested fields dierctly

GrossCost.new.
  has_cost.with_value("10").
  has_cost.with_article("shampoo").with_value=(4).build 
#=> which returns 
#{"costs":[
# {"article":"soap","value":10},
# {"article":"shampoo","value":4}
#]}

Array Object

The below endpoint return list of products, hence the response array of product object

#products.yml
request:
  url: products 
response:
  array_type: row
  body: '{
    "name": "soap"
    "id": 10
  }'
Products.new.build
Products.new.has_product.build
#=> return '[{"name":"soap","id":10}]'

Note: In above example, both the statements results the same response. By default, response assigns values as mentioned in config. And the first modifier "has_[singular_of_object]" does the same.

Using "has_[object_name]" method which takes array hash-map as parameter, to modify all or partial list of objects.

Products.new.
  has_products([{:name=>"tooth_paste"},{:name=>"pencil",:id=>2}]).build
#=> return '[{"name":"tooth_paste","id":10},
#            {"name":"pencil","id":2}]'

Using "has_[singular_of_object]" consecutively we can add multiple value, with partial and full assignment of list of fields.

Products.new.
  has_product(:name=>"tooth_paste").
  has_product({:name=>"pencil",:id=>2}).build
#=> return '[{"name":"tooth_paste","id":10},
#            {"name":"pencil","id":2}]'

Using "with_[filed_name]" modify value of field of array object

Products.new.
  has_product.with_name("tooth_paste").
  has_product.with_name("pencil").with_id(2).build
#=> return '[{"name":"tooth_paste","id":10},
#            {"name":"pencil","id":2}]'

Array Object with nested array

To be documented ...