In the world of automation testing, generating realistic-looking test data is a preferred approach to using static fixtures.

FakerMaker is a Ruby gem that allows you to do just that; using factories to create the test data you need. It can be used with Faker to dynamically generate test data.


We can use Faker and FakerMaker to create a factory for an API request body like this:

require 'faker'
require 'faker_maker'

FakerMaker.factory :example_request, naming: :json do
  name { }
  building_number { Faker::Address.building_number }
  street_name { Faker::Address.street_name }
  city { }
  country { }

Which can be called to create a request body like this:

body = FM[:example_request].build.as_json
=> {"name"=>"The Hon. Earleen Wunsch", 
    "streetName"=>"Mueller Lodge", 
    "city"=>"Port Darin", 

Often when testing we need to use specific values in the test data, however.

An example: we need to send a API request with “city” set to “London” and “country” set to “United Kingdom”.

Inefficient overwriting

This can be done in 4 steps by:

  1. Initialising a template request:
body = FM[:example_request].build.as_json
=> {"name"=>"Eleni Daniel", 
    "streetName"=>"Jacqulyn Lights", 
    "city"=>"New Rubin", 
    "country"=>"Isle of Man"}
  1. Overwriting the value of “city” in the request:
body["city"] = "London"
  1. Overwriting the value of “country” in the request:
body["country"] = "United Kingom"
  1. Sending a request containing the overwritten body:
=> {"name"=>"Eleni Daniel", 
  "streetName"=>"Jacqulyn Lights", 
  "country"=>"United Kingdom"}

Though this works, it can be considered “more expensive” than needed, as today I learned from a colleague a simple trick to do this more efficiently in just 2 steps…

Efficient initialising

  1. Initialising the request with the values we need for the relevant parameter:
body = 
  FM[:example_request].build(city: "London", country: "United Kingdom").as_json
  1. Sending a request containing the efficiently initialised body:
=> {"name"=>"Msgr. Ernesto Reynolds", 
"streetName"=>"Goyette Passage", 
"country"=>"United Kingdom"}

Job done. Initialising our test data was done more efficiently in a single line of code.

In the context of large automation test packs dealing with complex data, this simple trick can be particularly beneficial to improving efficiency.