Ackara / Mockaroo.NET

Mockaroo.NET is a portable class library that allows you to generate sample data based on a object type using the Mockaroo API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mockaroo.NET

version NuGet license

The Problem

You are working on a .NET project and you have to do some testing, but first you need load data into your program. Creating data by hand can be tedious, especially when you are at the phase where refactoring is frequent.

The Solution

Mockaroo.NET is a netstandard library that allows you to generate sample data based on your classes using the Mockaroo REST API.

Usage

Lets say you have the following class.

public class Employee
{
	public int Id { get; set; }
	public string Name { get; set; }
	public string Phone { get; set; } 
}

Now you want to generate a collection Employee objects to do some testing. All you have to do is.

var client = new MockarooClient(your_api_key);
IEnumerable<Employee> data = await client.FetchDataAsync<Employee>(records: 100);

The method will examine the Employee class properties, then generate objects using the data fetched from the Mockaroo Rest API. The data returned will look something like the following.

[{
	"Id": "156",
	"Name": "aliquam erat volutpat in congue etiam",
	"Phone": "adipiscing molestie hendrerit at vulputate"
}]

If Lorem Ipsum text is not to your liking, you can fine-tune the data by using the following.

var schema = new Schema<Employee>();
schema.Replace(x=> x.Name, DataType.FullName);
schema.Replace(x=> x.Phone, new PhoneField() { BlankPercentage = 50 });

var client = new MockarooClient(your_api_key);
IEnumerable<Employee> data = await client.FetchDataAsync<Employee>(schema, records: 1000);

The results will look like the following.

[{
	"Id": "156",
	"Name": "John Doe",
	"Phone": "(340) 123-4567"
}]

Currently there are over 140+ data types to choose from, check out the Mockaroo documentation to see the full list. You can also try it at mockaroo.com .

Reusing Data

The number of calls one can make to Mockaroo is limited, therefore it is a good idea to save and reuse the data retrieved from previous calls. The MockarooRepository class enables you to do just that. In addition, reusing data also means that the returned objects will be consistent instead of returning fresh data for each call. However keep in mind that if the Schema changes a new dataset will be retrieved.

Contributing

Prequistes:

Note: Run PS> .\build.ps1 configure to create the api key.

About

Mockaroo.NET is a portable class library that allows you to generate sample data based on a object type using the Mockaroo API.

License:MIT License


Languages

Language:C# 88.0%Language:PowerShell 12.0%