New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customizing AutoFixure using FromSeed Causes Exception #467
Comments
Crossposted on Stack Overflow: http://stackoverflow.com/q/33635042/126014 |
tl;drThe problem is that the factory function specified through BackgroundWhen customizing a type with the When AutoFixture populates the members of a class (such as properties and fields) it unwraps a Possible SolutionI found that modifying the + if (request != null && request.Equals(typeof(T))
+ {
+ return this.create(default(T));
+ }
var seededRequest = request as SeededRequest;
if (seededRequest == null)
{
return new NoSpecimen(request);
} I'm not sure whether this is the right answer, but at least it's a possibility. What do you guys think? CompatibilityRegardless of how we decide to solve it, this is going to be a breaking change. As such, it should be done in the |
The SeededFactory<T> class did not handle non-seeded requests for the specified type T. This resulted in an ObjectCreationException when AutoFixture had to create specimens to populate properties, fields and parameters of type T.
I tried to reduce the reported problem to this: [Fact]
public void FromSeedRepro()
{
var fixture = new Fixture();
fixture.Customize<Version>(x => x.FromSeed(s => s));
var v1 = fixture.Create<Version>();
var v2 = fixture.Create<Version>(new Version(2, 0));
var ph = fixture.Create<PropertyHolder<Version>>();
} The exception thrown looks like it's exactly the same exception as the one thrown by the code in http://stackoverflow.com/q/33619619/126014, for which I've recently submitted #470. I wonder if #470 would also solve this issue... |
I run that test separately in #469 and #470. It fails in both of them but with different exceptions. In #470 it fails with an
The reason why it fails in #469 is that the factory function However, the repro for this particular issue is slightly different: [Fact]
public void CustomizeFromSeedWillPopulatePropertyOfSameType()
{
// Fixture setup
var fixture = new Fixture();
var seed = new Version();
// Exercise system
fixture.Customize<Version>(c => c.FromSeed(s => seed));
// Verify outcome
Assert.Equal(seed, fixture.Create<PropertyHolder<Version>>().Property);
} In this case the factory function When I run it in #470 it fails with the same Now, this is where things get interesting. If I merge both #469 and #470 then both tests pass. I added them in 378991f for reference. |
The SeededFactory<T> class did not handle non-seeded requests for the specified type T. This resulted in an ObjectCreationException when AutoFixture had to create specimens to populate properties, fields and parameters of type T.
Given two classes:
I have set up the following test:
I can create
Foo
objects directly with and without seed values without any problem. But once I try to create aBar
object that has aFoo
property, I get anObjectCreationException
:I'd expect
TestFooFactory
to get passed anull
seed value during the creation ofBar
, just as when I createdFoo
without a seed value.In my real-world scenario, I want to customize how AutoFixture would use seeded values for certain objects when I pass seeded values in, but I still want AutoFixture to default to normal behavior if no seed is provided.
The text was updated successfully, but these errors were encountered: