Comments (4)
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.
closing the issue.
if in doubt about other features, please open a question in the discussions.
from autofixture.
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.
@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)
- β¨ Support FakeItEasy 8 HOT 1
- β¨ Support for NUnit 4 HOT 18
- π FromFactory doesn't honor the Range attribute HOT 6
- β¨ Accept `ISpecimentBuilder` as value generator for `With` HOT 1
- β¨ DataAnnotations support for specimen factories
- π Auto properties on large record types causes severe performance degradation HOT 4
- ICustomization for DateOnly causes a hang. HOT 1
- π RandomNumericSequenceGenerator with limits, can create values outside of these limits in certain scenarios HOT 1
- autofixture is missing NuGet package README file HOT 7
- autofixture.automoq is missing NuGet package README file HOT 1
- `IAsyncEnumerable` generates an infinite sequence π HOT 2
- π SourceLink doesn't work because of incorrect casing in GitHub URLs HOT 4
- π¨ Vulnerability in packages System.Net.Http and System.Text.RegularExpressions HOT 3
- Builder.WithVariations⨠HOT 4
- Not able to create second nullalbe int π HOT 3
- π Cannot create model with DateOnly field HOT 3
- π (v5) `MemberAutoData` and `ClassAutoData` override explicit `null` in data source HOT 2
- π v5 `MemberAutoData` does not find static members on base classes
- π AutoMoq fails to work with TimeProvider in .NET 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autofixture.