0

I'm evaluating Bogus faking entities in our domain model, but our implementation uses private setters in our domain model. Is there a way to set these properties with Faker? Perhaps an option to tell it to use reflection?

bdean20
  • 822
  • 8
  • 14

2 Answers2

1

I assume you're referring to https://github.com/bchavez/Bogus/

If I understand what you're asking, it "just works."

using Xunit;
using Bogus;

namespace SO54660453.Tests
{
   class ClassWithPrivateSetter
   {
      public string Name { get; private set; }
   }

   public class Tests
   {
      [Fact]
      public void TestClassWithPrivateSetter()
      {
         var faker = new Faker<ClassWithPrivateSetter>()
            .RuleFor(o => o.Name, f => f.Person.FullName);

         var testPoco = faker.Generate();

         Assert.False(string.IsNullOrEmpty(testPoco.Name));
      }
   }
}
xander
  • 1,689
  • 10
  • 18
1

Also, there is an opportunity to fake private field like this

var faker = new Faker<ClassWithPrivateSetter>()
            .RuleFor("_privateFieldName", f => f.Person.FullName);
My Name
  • 11
  • 1
  • 1