Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

c# - Force AutoFixture to use the greediest constructor

I have a data type with multiple constructors and I need AutoFixture to choose the greediest (one with the most parameters). The default behaviour is to choose the constructor with the smallest number.

The author's blog post, http://blog.ploeh.dk/2009/03/24/HowAutoFixtureCreatesObjects.aspx doesn't seem to imply there's a way of overriding this behaviour, so is it possible, and if so, how?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is certainly possible.

To change the strategy for a single type (MyClass):

fixture.Customize<MyClass>(c => c.FromFactory(
    new MethodInvoker(
        new GreedyConstructorQuery())));

To change the strategy across the board:

fixture.Customizations.Add(
    new MethodInvoker(
        new GreedyConstructorQuery()));

As it turns out, however, using GreedyConstructorQuery across the board is most likely problematic, as the following code snippet demonstrates. Imagine a class with this constructor:

public Foo(string name)
{
    this.name = name;
}

This test will throw an exception:

[Test]
public void GreedyConstructor()
{
    Fixture fixture = new Fixture();
    fixture.Customizations.Add(new MethodInvoker(new GreedyConstructorQuery()));

    Foo foo = fixture.CreateAnonymous<Foo>();
}

The exception thrown is:

Ploeh.AutoFixture.ObjectCreationException : AutoFixture was unable to create an instance from System.SByte*, most likely because it has no public constructor, is an abstract or non-public type.

So what's that about the SByte*? There's no SByte* in Foo...

Well, yes there is. By placing the MethodInvoker in Customization, it overrides all default creation strategies, including the one for strings. Instead, it goes looking for the greediest constructor for string and that is:

public String(sbyte* value, int startIndex, int length, Encoding enc);

And there's the sbyte*...


It's still possible to replace the modest constructor selection algorithm with a greedy algorithm, it's just a tad more involved than I first realized.

What you can do is this:

Write a small class like this one:

public class GreedyEngineParts : DefaultEngineParts
{
    public override IEnumerator<ISpecimenBuilder> GetEnumerator()
    {
        var iter = base.GetEnumerator();
        while (iter.MoveNext())
        {
            if (iter.Current is MethodInvoker)
                yield return new MethodInvoker(
                    new CompositeMethodQuery(
                        new GreedyConstructorQuery(),
                        new FactoryMethodQuery()));
            else
                yield return iter.Current;
        }
    }
}

and create the Fixture instance like this:

Fixture fixture = new Fixture(new GreedyEngineParts());

That should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...