❔ Looking for an AutoFixture struct customization workaround

Hello, Autofixture's .Do() customization doesn't work with value types which makes sense because nothing gets returned. I'm trying to have autofixture create a start time that is guaranteed to be before an end time within a value type. The following is what I have that works for reference types but not value types. If anyone has any work arounds to make autofixture generate start times before end time I would greatly appreciate it.
struct TimeRange
{
public Instant startTime;
public Instant endTime;
}
[Theory, AutoData]
public void StartEndTime([Range(1, 100)] int cardinality)
{
var fixture = new Fixture();
var timeRanges = fixture.Build<TimeRange>()
.Without(e => e.startTime)
.Without(e => e.endTime)
.Do(e =>
{
e.startTime = fixture.Create<Instant>();
e.endTime = e.startTime + fixture.Create<Duration>();
})
.CreateMany(cardinality)
.ToList();

timeRanges.Should().AllSatisfy(e =>
{
e.startTime.Should().BeLessThan(e.endTime);
});
}
struct TimeRange
{
public Instant startTime;
public Instant endTime;
}
[Theory, AutoData]
public void StartEndTime([Range(1, 100)] int cardinality)
{
var fixture = new Fixture();
var timeRanges = fixture.Build<TimeRange>()
.Without(e => e.startTime)
.Without(e => e.endTime)
.Do(e =>
{
e.startTime = fixture.Create<Instant>();
e.endTime = e.startTime + fixture.Create<Duration>();
})
.CreateMany(cardinality)
.ToList();

timeRanges.Should().AllSatisfy(e =>
{
e.startTime.Should().BeLessThan(e.endTime);
});
}
1 Reply
Accord
Accord9mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.