You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just want to let you know that the example on the readme page seems outdated.
I think the main example should work by copying/pasting it in a console application.
However, as of now, the example is not complete. (It is missing the User class and the Dump method)
Also, the example on the readme is using 'LotNumber' but 'Lot number' has been commented in the unit test.
Here is my revised version:
using System;
using System.Collections.Generic;
using Bogus;
using Newtonsoft.Json;
namespace UsageExample
{
public class Program
{
static void Main(string[] args)
{
//Set the randomzier seed if you wish to generate repeatable data sets.
Randomizer.Seed = new Random(3897234);
var fruit = new[] { "apple", "banana", "orange", "strawberry", "kiwi" };
var orderIds = 0;
var testOrders = new Faker<Order>()
//Ensure all properties have rules. By default, StrictMode is false
//Set a global policy by using Faker.DefaultStrictMode if you prefer.
.StrictMode(true)
//OrderId is deterministic
.RuleFor(o => o.OrderId, f => orderIds++)
//Pick some fruit from a basket
.RuleFor(o => o.Item, f => f.PickRandom(fruit))
//A random quantity from 1 to 10
.RuleFor(o => o.Quantity, f => f.Random.Number(1, 10));
//A nullable int? with 80% probability of being null.
//The .OrNull extension is in the Bogus.Extensions namespace.
//.RuleFor(o => o.LotNumber, f => f.Random.Int(0, 100).OrNull(f, .8f));
var userIds = 0;
var testUsers = new Faker<User>()
//Optional: Call for objects that have complex initialization
.CustomInstantiator(f => new User(userIds++, f.Random.Replace("###-##-####")))
//Basic rules using built-in generators
.RuleFor(u => u.FirstName, f => f.Name.FirstName())
.RuleFor(u => u.LastName, f => f.Name.LastName())
.RuleFor(u => u.Avatar, f => f.Internet.Avatar())
.RuleFor(u => u.UserName, (f, u) => f.Internet.UserName(u.FirstName, u.LastName))
.RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FirstName, u.LastName))
.RuleFor(u => u.SomethingUnique, f => $"Value {f.UniqueIndex}")
.RuleFor(u => u.SomeGuid, Guid.NewGuid)
//Use an enum outside scope.
.RuleFor(u => u.Gender, f => f.PickRandom<Gender>())
//Use a method outside scope.
.RuleFor(u => u.CartId, f => Guid.NewGuid())
//Compound property with context, use the first/last name properties
.RuleFor(u => u.FullName, (f, u) => u.FirstName + " " + u.LastName)
//And composability of a complex collection.
.RuleFor(u => u.Orders, f => testOrders.Generate(3))
//After all rules are applied finish with the following action
.FinishWith((f, u) => { Console.WriteLine("User Created! Name={0}", u.FullName); });
var user = testUsers.Generate(3);
user.Dump();
}
}
public class Order
{
public int OrderId { get; set; }
public string Item { get; set; }
public int Quantity { get; set; }
}
public enum Gender
{
Male,
Female
}
public class User
{
public User(int userId, string ssn)
{
this.Id = userId;
this.SSN = ssn;
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string SomethingUnique { get; set; }
public Guid SomeGuid { get; set; }
public string Avatar { get; set; }
public Guid CartId { get; set; }
public string SSN { get; set; }
public Gender Gender { get; set; }
public List<Order> Orders { get; set; }
}
public static class ExtensionsForTesting
{
public static void Dump(this object obj)
{
Console.WriteLine(obj.DumpString());
}
public static string DumpString(this object obj)
{
return JsonConvert.SerializeObject(obj, Formatting.Indented);
}
}
}
/* OUTPUT:
User Created! Name=Laila Luettgen
User Created! Name=Aracely King
User Created! Name=Ryder Purdy
[
{
"Id": 0,
"FirstName": "Laila",
"LastName": "Luettgen",
"FullName": "Laila Luettgen",
"UserName": "Laila99",
"Email": "Laila52@gmail.com",
"SomethingUnique": "Value 0",
"SomeGuid": "bd3b9e5b-fab4-4d69-826a-038a33894a06",
"Avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/maximsorokin/128.jpg",
"CartId": "78eb8304-1b46-47cc-a4d0-9ac58ed31c71",
"SSN": "485-67-8799",
"Gender": 0,
"Orders": [
{
"OrderId": 0,
"Item": "banana",
"Quantity": 4
},
{
"OrderId": 1,
"Item": "banana",
"Quantity": 4
},
{
"OrderId": 2,
"Item": "apple",
"Quantity": 7
}
]
},
{
"Id": 1,
"FirstName": "Aracely",
"LastName": "King",
"FullName": "Aracely King",
"UserName": "Aracely74",
"Email": "Aracely.King@gmail.com",
"SomethingUnique": "Value 4",
"SomeGuid": "711cf33a-dc98-45e4-b047-808a26a325eb",
"Avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bungiwan/128.jpg",
"CartId": "7dcd43cf-a7ad-4998-a82c-fe4acb8f58fa",
"SSN": "821-82-8244",
"Gender": 0,
"Orders": [
{
"OrderId": 3,
"Item": "strawberry",
"Quantity": 9
},
{
"OrderId": 4,
"Item": "kiwi",
"Quantity": 7
},
{
"OrderId": 5,
"Item": "orange",
"Quantity": 2
}
]
},
{
"Id": 2,
"FirstName": "Ryder",
"LastName": "Purdy",
"FullName": "Ryder Purdy",
"UserName": "Ryder_Purdy",
"Email": "Ryder35@hotmail.com",
"SomethingUnique": "Value 8",
"SomeGuid": "7babf112-baec-4f50-af6c-3218de7151c2",
"Avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/nilshelmersson/128.jpg",
"CartId": "51efa117-9a8b-4824-adbb-61a8c69f94fb",
"SSN": "315-87-9007",
"Gender": 0,
"Orders": [
{
"OrderId": 6,
"Item": "kiwi",
"Quantity": 2
},
{
"OrderId": 7,
"Item": "orange",
"Quantity": 10
},
{
"OrderId": 8,
"Item": "kiwi",
"Quantity": 8
}
]
}
]
*/
This example should work out of the box if you have Bogus and Newtonsoft package installed.
I think it will give a better first impression if we don`t have to fix the first example.
Thanks
The text was updated successfully, but these errors were encountered:
Hello,
I just want to let you know that the example on the readme page seems outdated.
I think the main example should work by copying/pasting it in a console application.
However, as of now, the example is not complete. (It is missing the User class and the Dump method)
Also, the example on the readme is using 'LotNumber' but 'Lot number' has been commented in the unit test.
Here is my revised version:
This example should work out of the box if you have Bogus and Newtonsoft package installed.
I think it will give a better first impression if we don`t have to fix the first example.
Thanks
The text was updated successfully, but these errors were encountered: