csharp AutoFixture策略,用于调用基于Twitter讨论的大多数参数的工厂方法https://twitter.com/madstt/status/4208

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp AutoFixture策略,用于调用基于Twitter讨论的大多数参数的工厂方法https://twitter.com/madstt/status/4208相关的知识,希望对你有一定的参考价值。

internal class GreedyFactoryMethodQuery : IMethodQuery
{
    public IEnumerable<IMethod> SelectMethods(Type type)
    {
        if (type == null)
            throw new ArgumentNullException("type");

        return from mi in type.GetMethods(
                    BindingFlags.Static | BindingFlags.Public)
                where mi.ReturnType == type
                let parameters = mi.GetParameters()
                orderby parameters.Length descending
                select new StaticMethod(mi) as IMethod;
    }
}

public class ATypeWithFactoryMethods
{
    private ATypeWithFactoryMethods()
    {
    }

    public static ATypeWithFactoryMethods Create()
    {
        return new ATypeWithFactoryMethods();
    }

    public static ATypeWithFactoryMethods Create(object argument)
    {
        return new ATypeWithFactoryMethods();
    }
}

[Fact]
public void Test()
{
    var fixture = new Fixture();

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

    var result = fixture.Create<ATypeWithFactoryMethods>();
    // -> AutoFixture invokes the static Factory Method with most parameters.
}

以上是关于csharp AutoFixture策略,用于调用基于Twitter讨论的大多数参数的工厂方法https://twitter.com/madstt/status/4208的主要内容,如果未能解决你的问题,请参考以下文章

.net测试篇之单元测试/集成测试神器Autofixture

.net测试篇之单元测试/集成测试神器Autofixture

如何让 AutoFixture 创建一个大于 0 的整数,而不是另一个数字?

.net测试篇之测试神器Autofixture Generator使用与自定义builder

csharp 用于处理DataTables.net客户端表呈现插件的Ajax调用的C#代码。

csharp 事件源聚合策略样本