nickdodd79 / AutoBogus

A C# library complementing the Bogus generator by adding auto creation and population capabilities.
MIT License
423 stars 49 forks source link

AutoFaker[T] Clone Breaking #102

Open codyspeck-ppa opened 1 year ago

codyspeck-ppa commented 1 year ago

Calling the underlying Faker<T>.Clone method on an AutoFaker<T> instance causes Generate to fail. Is this by design or a bug?

using AutoBogus;
using FluentAssertions;
using Xunit;

namespace AutoBogusSandbox;

public class Dog
{
    public string Name { get; set; }
}

public class Tests
{
    [Fact]
    public void Calling_generate_after_clone_should_not_fail()
    {
        var faker = new AutoFaker<Dog>();

        var clone = faker.Clone();

        clone.Invoking(x => x.Generate())
            .Should().NotThrow();
    }
}

In any case, is there a feature supported by AutoBogus that allows me to define a common set of AutoFaker instances that can be overridden per-test? This is how I am trying to accomplish that currently:

using AutoBogus;
using FluentAssertions;
using Xunit;

namespace AutoBogusSandbox;

public class Dog
{
    public string Name { get; set; }
}

public static class Fakers
{
    public static AutoFaker<Dog> Dog = new();
}

public class Tests
{
    [Fact]
    public void Dog_is_named_sally()
    {
        var dog = Fakers.Dog.Clone()
            .RuleFor(x => x.Name, () => "Sally")
            .Generate();

        dog.Name.Should().Be("Sally");
    }
}