Mocking Data
Introduction
Since version 4.x we have included as a dependency MockDataCFC. MockDataCFC is a simple service to generate fake data as a JSON REST service, a ColdBox Module or a simple CFC Service API. The idea being that you may be offline, may not have access to an API, or simply need some fake data to test on your front end or seed a complete database with fake data.
MockDataCFC allows you to define the return data model in a very deterministic and simple modeling DSL. Read on 🚀 for some modeling goodness!
mockData()
Every test bundle has access to our mocking method: mockData()
which internally calls the mock()
method in the MockDataCFC
module. You will pass all the arguments that you need to produce beautiful mocked data.
Available Mocking Types
The available types MockDataCFC supports are:
age
: Generates a random "adult" age of 18 to 75.all_age
: Generates a random age of 1 to 100.autoincrement
: Returns an incremented index starting from 1baconlorem
: Returns bacon lorem ipsum text. If used asbaconlorem:N
, returns N paragraphs. If used asbaconlorem❌ Y
, returns a random number of paragraphs between X and Y.date
: Generates a random datedatetime
: Generates a random date and time valueemail
: Generates a random email.fname
: Generates a random first name.imageurl
: Generates a random image URL with a random protocolimageurl_http
: Generates a random image URL withhttp
only protocolimageurl_https
: Generates a random image URL withhttps
only protocolipaddress
: Generates an ipv4 addressname
: Generates a random name.lname
: Generates a random last name.lorem
: Returns lorem ipsum text. If used aslorem:N
, returns N paragraphs. If used aslorem❌ Y
, returns a random number of paragraphs between X and Y.num
: By default, a number from 1 to 10. You can also use the formnum:X
for a random number between 1 and X. Ornum❌ Y
for a random number between X and Y.oneof❌ y
: Requires you to pass N values after it delimited by a colon. Example:oneof:male: female
. Will return a random value from that list.rnd:N
,rand:N
,rnd❌ y
,rand❌ y
: Generate random numbers with a specific range or range cap.sentence
: Generates a sentences. If used assentence:N
, returns N sentences. If used assentence❌ Y
, returns a random number of sentences beetween X and Y.ssn
: Generates a random Social Security number.string
: Generates a random string of length 10 by default. You can increase the length by passing itstring:length
.tel
: Generates a random (American) telephone number.uuid
: Generates a random UUIDurl
: Generates a random URL with a random protocolurl_http
: Generates a random URL withhttp
only protocolurl_https
: Generates a random URL withhttps
only protocolwebsite
: Generates a random website with random protocolwebsite_http
: Generates a random website,http
only protocolwebsite_https
: Generates a random website,https
only protocolwords
: Generates a single word. If used asword:N
, returns N words. If used aswords❌ Y
, returns a random number of words beetween X and Y.
Supplier Type (Custom Data)
You can also create your own content by using a supplier closure/lambda as your type. This is a function that will create the content and return it for you.
The function receives the currently iterating index
as an argument as well. All you need to do is return back content.
Examples
Let's imagine the following object graph:
I can then use this mocking DSL to define it:
More information here: https://www.forgebox.io/view/mockdatacfc
Last updated