Home > Net >  AutoFixture: how to create property's value without prefix
AutoFixture: how to create property's value without prefix

Time:08-13

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");
}

CodePudding user response:

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>();
  • Related