Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The example on the readme page seems outdated. #330

Closed
ssinfod opened this issue Oct 8, 2020 · 0 comments
Closed

The example on the readme page seems outdated. #330

ssinfod opened this issue Oct 8, 2020 · 0 comments

Comments

@ssinfod
Copy link

ssinfod commented Oct 8, 2020

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:

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

bchavez added a commit that referenced this issue Oct 11, 2020
… to adding LotNumber. Removed redundant unit test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant