Git Product home page Git Product logo

Comments (4)

aivascu avatar aivascu commented on September 26, 2024

Hi @grzegorz-wolszczak,
The behavior you are observing is not a bug. FromSeed works similar to the FromFactory method in the builder API.
It will provide a way to create an object instance from a seed but will not prevent default behavior.
I your case since the Foo.StrVal has a public setter the value in that property will be overridden by the initialization of auto-properties. To avoid it you could either use 'OmitAutoProperties()or explicitly omit that specific member.Without(x => x.StrVal)`.

Here's a working example:

[Fact]
public void TestFromSeed()
{
    // Arrange
    var fixture = new Fixture();
    var fooSeed = new Foo() { StrVal = "FromSeed", };

    fixture.Customize<Foo>(c => c
        .FromSeed(seed => new Foo() { StrVal = seed.StrVal })
        .Without(x => x.StrVal));

    // Act
    var fooFromSeed = fixture.Create<Foo>(fooSeed);

    // Assert
    Assert.Equal("FromSeed", fooFromSeed.StrVal);
}

Regarding the documentation. I know there isn't much to go on.
At the moment the project priorities lie in other areas. If you wish to help you could raise a PR in the documentation project.

from autofixture.

aivascu avatar aivascu commented on September 26, 2024

closing the issue.
if in doubt about other features, please open a question in the discussions.

from autofixture.

Kralizek avatar Kralizek commented on September 26, 2024

I have been often puzzled by the from seed method because its behavior feels unnatural or, at the very least, conflicting with the intent declared by the name.

In my opinion, an object constructed from a seed should be equivalent to the seed unless explicitly customized otherwise.

I'd consider changing the method so that OmitAutoProperties is automatically included

It will then be responsibility of the user to specify With(property) to force the generation of a new value for the specific property.

from autofixture.

aivascu avatar aivascu commented on September 26, 2024

@Kralizek I can only assume, but I think the intention was to allow creation of related values. Using the original object as a prototype.
For example objects with the same foreign key, people with the same last name, etc.
To create a new object you'd create a specimen builder that would handle a seeded request for a specific type.
Then in that handler you'd specify how the value is created and return it. No other alterations would be made to that object, since the resulting object was created.
It's the fact that @grzegorz-wolszczak used the builder API that forced the other properties to be filled in.

For example:

[Fact]
public void TestFromSeed()
{
    // Arrange
    var fixture = new Fixture();
    fixture.Customizations.Add(new SeededFooBuilder());
    var fooSeed = new Foo { StrVal = "FromSeed" };

    // Act
    var fooFromSeed = fixture.Create<Foo>(fooSeed);

    // Assert
    Assert.Equal(fooSeed.StrVal, fooFromSeed.StrVal);
    Assert.Null(fooFromSeed.OtherStrVal);
}

public class Foo
{
    public string StrVal { get; set; }
    public string OtherStrVal { get; set; }
}

public class SeededFooBuilder : ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        if (request is not SeededRequest seededRequest)
        {
            return new NoSpecimen();
        }

        if (seededRequest.Seed is not Foo foo)
        {
            return new NoSpecimen();
        }

        return new Foo { StrVal = foo.StrVal };
    }
}

I don't think the behavior should change, if it would then the the builder API would become inconsistent.

from autofixture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.