2

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");
}
valker
  • 123
  • 1
  • 2
  • 15
  • FromFactory isn't used for this purpose and doesn't work to this effect, see https://github.com/AutoFixture/AutoFixture/issues/898 – politus Oct 05 '22 at 20:05

1 Answers1

3

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>();
Red Star
  • 556
  • 3
  • 13
  • Is it possible to disable prefixing with property names globally? – mrt181 Dec 22 '22 at 08:56
  • @mrt181, as I know you can't do it, but there are customization classes, you can write them on your own and reuse hhttps://stackoverflow.com/questions/38688932/how-to-use-autofixture-to-build-with-customized-properties-while-keeping-type-cu – Red Star Dec 22 '22 at 17:55