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

Non-generic version of Create method #97

Closed
dhilgarth opened this issue Apr 26, 2013 · 6 comments
Closed

Non-generic version of Create method #97

dhilgarth opened this issue Apr 26, 2013 · 6 comments

Comments

@dhilgarth
Copy link
Contributor

@dhilgarth dhilgarth commented Apr 26, 2013

Is there a simple way to create an anonymous instance from a Type instance?
Looking through the code, I came up with this:

    public static object Create(this ISpecimenBuilder builder, Type type)
    {
        return new SpecimenContext(builder).Resolve(new SeededRequest(type, null));
    }

Is this correct? Is there a simpler way to do it? If not, would it be benefical to add this to AutoFixture?

@moodmosaic
Copy link
Member

@moodmosaic moodmosaic commented Apr 26, 2013

Perhaps this might help:

public static object Create(this Fixture fixture, Type type)
{
    var context = new SpecimenContext(fixture.Compose());
    return context.Resolve(type);
}

The code is from from this post by @ThomasArdal.

@dhilgarth
Copy link
Contributor Author

@dhilgarth dhilgarth commented Apr 26, 2013

Thanks. What's the benefit of calling Compose?

@ThomasArdal
Copy link

@ThomasArdal ThomasArdal commented Apr 26, 2013

Correct. I originaly wrote about this here: http://thomasardal.com/non-generic-createanonymous-method-for-autofixture/

But! Fixture were changed in AutoFixture 3.x. You now simply need to do this instead (no Compose):

public static object Create(this Fixture fixture, Type type)
{
  var context = new SpecimenContext(fixture);
  return context.Resolve(type);
}

@dhilgarth
Copy link
Contributor Author

@dhilgarth dhilgarth commented Apr 26, 2013

Looks exactly like what I have - just with the difference that you use Fixture instead of the more general ISpecimenBuilder. As far as I can see there is no benefit in constraining this extension method to Fixture as the only constructor of SpecimenContext takes an ISpecimenBuilder.
Am I missing something?

@ThomasArdal
Copy link

@ThomasArdal ThomasArdal commented Apr 26, 2013

You are right. Fixture now implements ISpecimenBuilder. Constraining it to Fixture, doesn't make sense :)

@dhilgarth
Copy link
Contributor Author

@dhilgarth dhilgarth commented Apr 26, 2013

Thanks for the confirmation! :)

@dhilgarth dhilgarth closed this Apr 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants