Search code examples
c#autofixture

AutoFixture: how to create property's value without prefix


For my test I need to have the Name property contains just a Guid's representaion without prefixed property's name. I've tried to solve with FromFactory method (see comment in the code) but had no success.

private class A
{
    public string Name { get; set; } = null!;
}

[Fact]
public void Test()
{
    Fixture fixture = new();
    // var a = fixture.Build<A>().FromFactory(() => new() { Name = Guid.NewGuid().ToString() }).Create<A>();
    var a = fixture.Create<A>();
    a.Name.Should().NotStartWith("Name");
}

Solution

  • First, you need to customize:

    fixture.Customize<A>(c => c.With(x => x.Name, Guid.NewGuid().ToString()));
    

    Then, simply create:

    var myMarvelousVariable = fixture.Create<A>();