Skip to content
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

Support for value types with default constructors #19

Closed
ploeh opened this issue Oct 18, 2012 · 4 comments
Closed

Support for value types with default constructors #19

ploeh opened this issue Oct 18, 2012 · 4 comments

Comments

@ploeh
Copy link
Member

ploeh commented Oct 18, 2012

AutoFixture should optionally support value types with default constructors. See this discussion for more details.

Since I believe in the GOOS paradigm, I think it's a beneficial feature that AutoFixture OOB chokes on such constructs, but we should consider elaborating on this potential issue in the error message. E.g. instead of saying

AutoFixture was unable to create an instance from Foo, most likely because it has no public constructor, is an abstract or non-public type.

we should consider adding more information - e.g.

AutoFixture was unable to create an instance from Foo, since it's a value type with no explicit, parameterized constructors. Are you attempting to create an instance of a mutable value type? If so, you should strongly consider changing the design of the value type. However, if you are unable to do so, you can add the SupportValueTypeDefaultConstructors customizations to your Fixture instance:

var fixture = new Fixture().Customize(new SupportValueTypeDefaultConstructors());

This would obviously also require us to add said SupportValueTypeDefaultConstructors customization to the AutoFixture code base.

@Vidarls
Copy link

Vidarls commented Mar 11, 2013

Possibly related: The ability to set the value of individual properties on an immutable type where all setters are private.

//This will result in a compile error because of inaccessible setter
//If the "newValue" was injected through the constructor this would work
var x = fixture.Build<MyImmutableClass>()
  .With(v => v.MyReadonlyPropery = "newValue")
  .CreateAnonymous();

@WojcikMike
Copy link
Contributor

I am trying to implement this one. Implementing checking for custom structs without constructors went well. However I have issues with allowing for that unsupported operation.

Initial code can be found here:
WojcikMike@ebda500

Short description goes as follows:
To Fixture I've added something like this:
new FilteringSpecimenBuilder(
new StructureWithoutConstructorSpecimenBuilder(), //throws exception with detailed message
new AndRequestSpecification(
new StructureSpecification(), //checks if it is custom struct
new NoConstructorSpecification())) //checks if it doesn't have constructors

I've created two classes:
SupportStructureDefaultConstructorsCustomization, SupportStructureDefaultConstructorsGenerator. and this is where the tricky parts are.

Generator creates structure itself so it is fine however I would have to pass it to fill all properties. To do this ideally my generator would have to land somewhere before Postprocessor with AutoPropertiesTarget. Any advices how to proceed?

@ploeh
Copy link
Member Author

ploeh commented May 30, 2013

The Auto-Properties feature is implemented by composing Postprocessor with an AutoPropertiesCommand, so your customization would need to do something like this:

fixture.Customizations.Add(
    new Postprocessor(
        new SupportStructureDefaultConstructorsGenerator(),
        new AutoPropertiesCommand());

You may need to use the Postprocessor constructor overload, that also takes as a third parameter an IRequestSpecification...

@ploeh
Copy link
Member Author

ploeh commented Jun 1, 2013

Available in AutoFixture 3.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants