Bogus Mocking Data Same Values For 2 different properties
Asked Answered
A

3

10

Bogus Mocking Data Same Values For 2 different properties is it possible to have 2 properties have same value in the fluent api.

var users = new Faker<User>()
   .StrictMode(false)
   .RuleFor(o => o.Id, f => orderIds++)
   .RuleFor(o => o.UserName, f => f.Person.FullName) // This needs to be same as the next property
   .RuleFor(o => o.NormalizedUserName, f => f.Person.FullName) // This should be same but uppercase

Wanted generated data:

[
  {
   userName: "Ivan Horvat",
   normalizedUserName: "IVAN HORVAT"
  },
  {
   userName: "John Doe",
   normalizedUserName: "JOHN DOE"
  }
]

I want each of the entities generated to have same UserName and NormalizedUsername but each entity its own.

Accrual answered 22/11, 2019 at 14:35 Comment(0)
R
17

You can also have two properties with the same value by using the RuleFor(Prop, (f, usr) =>) overload too.

void Main()
{
   int orderIds = 0;
   var users = new Faker<User>()
      .StrictMode(false)
      .RuleFor(o => o.Id, f => orderIds++)
      .RuleFor(o => o.UserName, f => f.Person.FullName) // This needs to be same as the next property
      .RuleFor(o => o.NormalizedUserName, (f, usr) => usr.UserName.ToUpper()); // This should be same but uppercase

   users.Generate(3).Dump();
}

public class User{
   public int Id{get;set;}
   public string UserName{get;set;}
   public string NormalizedUserName {get;set;}
}

results

Rugby answered 23/11, 2019 at 2:4 Comment(2)
WOW, this is a great feature!Dukedom
Thanks! Very useful answerDinh
M
5

The following works as expected

[TestClass]
public class MyTestClass1 {
    [TestMethod]
    public void MyTestMethod() {
        //Arrange
        int orderIds = 0;

        var faker = new Faker<User>()
           .StrictMode(false)
           .RuleFor(o => o.Id, f => ++orderIds)
           .RuleFor(o => o.UserName, f => f.Person.FullName) // This needs to be same as the next property
           .RuleFor(o => o.NormalizedUserName, f => f.Person.FullName.ToUpper()) // This should be same but uppercase
           .RuleFor(o => o.Email, f => $"{f.Person.FirstName}.{f.Person.LastName}@company.com");
        //Act
        var user = faker.Generate();

        //Assert
        user.UserName.ToUpper().Should().Be(user.NormalizedUserName);
    }

    public class User {
        public int Id { get; internal set; }
        public string UserName { get; internal set; }
        public string NormalizedUserName { get; internal set; }
        public string Email { get; internal set; }
    }
}

All the created instance have the desired values as indicated in the comments. Note the use of ToUpper() for the NormalizedUserName

Muco answered 22/11, 2019 at 15:4 Comment(0)
C
2

Another pattern that could be helpful. Here we prepare a list of source data (data) - either load from DB or prepare manually. Bogus selects a random item from the source data (randomRecord) and uses it to fill various fields of a target YourEntity list item.

var data = new List<SomeClass>; // Load from DB a list of data as source

var faker = new Faker<YourEntity>();
faker.StrictMode(false);
faker.Rules((setter, entity) =>
{
    var randomRecord = setter.PickRandom(data); // Here we pick random data from our source collection

    entity.Field1 = randomRecord.SomeField1;
    entity.Field2 = randomRecord.SomeField2;
    entity.Field3 = $"{entity.Field1} {entity.Field2}";
});
Containment answered 25/9, 2023 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.