c# - 使用 AutoFixture 为递归数据结构创建夹具

标签 c# .net unit-testing autofixture

我正在从事一个项目,其中我有一些递归数据结构,我想为它创建一个夹具。

数据结构是 XmlCommandElement,它有一个方法 ToCommandXmlCommandElement 转换为 Command

树上的每个节点都可以是 XmlCommandElement 和/或 XmlCommandPropertyElement

现在,为了测试 ToCommand 方法的行为,我想用一些任意数据获取 XmlCommandElement

我想控制树的深度以及每个节点的 XmlCommandElement 和/或 XmlCommandPropertyElement 实例的数量。

下面是我用于夹具的代码:

public class XmlCommandElementFixture : ICustomization
{
    private static readonly Fixture _fixture = new Fixture();

    private XmlCommandElement _xmlCommandElement;

    public int MaxCommandsPerDepth { get; set; }

    public int MaxDepth { get; set; }

    public int MaxPropertiesPerCommand { get; set; }

    public XmlCommandElementFixture BuildCommandTree()
    {
        _xmlCommandElement = new XmlCommandElement();

        var tree = new Stack<XmlCommandElementNode>();

        tree.Push(new XmlCommandElementNode(0, _xmlCommandElement));

        while (tree.Count > 0) {
            var node = tree.Pop();
            node.Command.Key = CreateRandomString();
            node.Command.Properties = CreateProperties();

            if (MaxDepth > node.Depth) {
                var commands = new List<XmlCommandElement>();

                for (var i = 0; i < MaxCommandsPerDepth; i++) {
                    var command = new XmlCommandElement();
                    tree.Push(new XmlCommandElementNode(node.Depth + 1, command));
                    commands.Add(command);
                }

                node.Command.Commands = commands.ToArray();
            }
        }

        return this;
    }

    public void Customize(IFixture fixture)
    {
        fixture.Customize<XmlCommandElement>(c => c.FromFactory(() => _xmlCommandElement)
                                                   .OmitAutoProperties());
    }

    private static string CreateRandomString()
    {
        return _fixture.Create<Generator<string>>().First();
    }

    private XmlCommandPropertyElement[] CreateProperties()
    {
        var properties = new List<XmlCommandPropertyElement>();

        for (var i = 0; i < MaxPropertiesPerCommand; i++) {
            properties.Add(new XmlCommandPropertyElement {
                Key = CreateRandomString(),
                Value = CreateRandomString()
            });
        }

        return properties.ToArray();
    }

    private struct XmlCommandElementNode
    {
        public XmlCommandElementNode(int depth, XmlCommandElement xmlCommandElement)
        {
            Depth = depth;

            Command = xmlCommandElement;
        }

        public XmlCommandElement Command { get; }

        public int Depth { get; }
    }
}

这就是我使用它的方式:

xmlCommandElement = new Fixture().Customize(new XmlCommandElementFixture {
    MaxDepth = 2,
    MaxCommandsPerDepth = 3,
    MaxPropertiesPerCommand = 4
}.BuildCommandTree()).Create<XmlCommandElement>();

这工作得很好!但我遇到的问题是它不是通用的,至少据我所知,AutoFixture 的全部意义在于避免制作特定的夹具。

所以我真正想做的是这样的事情(找到它 here 但它对我不起作用。):

var fixture = new Fixture();
fixture.Behaviors.OfType<ThrowingRecursionBehavior>()
       .ToList()
       .ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new DepthThrowingRecursionBehavior(2));
fixture.Behaviors.Add(new OmitOnRecursionForRequestBehavior(typeof(XmlCommandElement), 3));
fixture.Behaviors.Add(new OmitOnRecursionForRequestBehavior(typeof(XmlCommandPropertyElement), 4));

xmlCommandElement = fixture.Create<XmlCommandElement>();

所有代码供引用:

接口(interface):

public interface ICommandCollection : IEnumerable<ICommand>
{
    ICommand this[string commandName] { get; }

    void Add(ICommand command);
}

public interface ICommandPropertyCollection : IEnumerable<ICommandProperty>
{
    string this[string key] { get; }

    void Add(ICommandProperty property);
}

public interface ICommandProperty
{
    string Key { get; }

    string Value { get; }
}

public interface ICommand
{
    ICommandCollection Children { get; set; }

    string Key { get; }

    ICommandPropertyCollection Properties { get; }
}

public interface ICommandConvertible
{
    ICommand ToCommand();
}

类:

public sealed class CommandPropertyCollection : ICommandPropertyCollection
{
    private readonly IDictionary<string, ICommandProperty> _properties;

    public CommandPropertyCollection()
    {
        _properties = new ConcurrentDictionary<string, ICommandProperty>();
    }

    public string this[string key]
    {
        get
        {
            ICommandProperty property = null;

            _properties.TryGetValue(key, out property);

            return property.Value;
        }
    }

    public void Add(ICommandProperty property)
    {
        _properties.Add(property.Key, property);
    }

    public IEnumerator<ICommandProperty> GetEnumerator()
    {
        return _properties.Values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public sealed class CommandProperty : ICommandProperty
{
    public CommandProperty(string key, string value)
    {
        Key = key;

        Value = value;
    }

    public string Key { get; }

    public string Value { get; }
}

public sealed class Command : ICommand
{
    public Command(string key, ICommandPropertyCollection properties)
    {
        Key = key;

        Properties = properties;
    }

    public ICommandCollection Children { get; set; }

    public string Key { get; }

    public ICommandPropertyCollection Properties { get; }
}

public class XmlCommandPropertyElement : ICommandPropertyConvertible
{
    [XmlAttribute("key")]
    public string Key { get; set; }

    [XmlAttribute("value")]
    public string Value { get; set; }

    public ICommandProperty ToCommandProperty()
    {
        return new CommandProperty(Key, Value);
    }
}

最后,我要测试的类如下:

public class XmlCommandElement : ICommandConvertible
{
    [XmlArray]
    [XmlArrayItem("Command", typeof(XmlCommandElement))]
    public XmlCommandElement[] Commands { get; set; }

    [XmlAttribute("key")]
    public string Key { get; set; }

    [XmlArray]
    [XmlArrayItem("Property", typeof(XmlCommandPropertyElement))]
    public XmlCommandPropertyElement[] Properties { get; set; }

    public ICommand ToCommand()
    {
        ICommandPropertyCollection properties = new CommandPropertyCollection();

        foreach (var property in Properties) {
            properties.Add(property.ToCommandProperty());
        }

        ICommand command = new Command(Key, properties);

        return command;
    }
}

测试本身看起来像这样:

namespace Yalla.Tests.Commands
{
    using Fixtures;

    using FluentAssertions;

    using Ploeh.AutoFixture;

    using Xbehave;

    using Yalla.Commands;
    using Yalla.Commands.Xml;

    public class XmlCommandElementTests
    {
        [Scenario]
        public void ConvertToCommand(XmlCommandElement xmlCommandElement, ICommand command)
        {
            $"Given an {nameof(XmlCommandElement)}"
                .x(() =>
                {
                    xmlCommandElement = new Fixture().Customize(new XmlCommandElementFixture {
                        MaxDepth = 2,
                        MaxCommandsPerDepth = 3,
                        MaxPropertiesPerCommand = 4
                    }.BuildCommandTree()).Create<XmlCommandElement>();
                });

            $"When the object is converted into {nameof(ICommand)}"
                .x(() => command = xmlCommandElement.ToCommand());

            "Then we need to have a root object with a key"
                .x(() => command.Key.Should().NotBeNullOrEmpty());

            "And 4 properties as its children"
                .x(() => command.Properties.Should().HaveCount(4));
        }
    }
}

感谢 Mark Seemann!最终的解决方案如下所示:

public class RecursiveCustomization : ICustomization
{
    public int MaxDepth { get; set; }

    public int MaxElements { get; set; }

    public void Customize(IFixture fixture)
    {
        fixture.Behaviors
               .OfType<ThrowingRecursionBehavior>()
               .ToList()
               .ForEach(b => fixture.Behaviors.Remove(b));
        fixture.Behaviors.Add(new OmitOnRecursionBehavior(MaxDepth));
        fixture.RepeatCount = MaxElements;
    }
}

并且可以这样使用:

xmlCommandElement = new Fixture().Customize(new RecursiveCustomization {
    MaxDepth = 2,
    MaxElements = 3
}).Create<XmlCommandElement>();

最佳答案

您可以通过更改 Fixture 的递归行为相当轻松地创建一棵小树:

[Fact]
public void CreateSmallTree()
{
    var fixture = new Fixture();
    fixture.Behaviors
        .OfType<ThrowingRecursionBehavior>()
        .ToList()
        .ForEach(b => fixture.Behaviors.Remove(b));
    fixture.Behaviors.Add(new OmitOnRecursionBehavior(recursionDepth: 2));

    var xce = fixture.Create<XmlCommandElement>();

    Assert.NotEmpty(xce.Commands);
}

以上测试通过。

关于c# - 使用 AutoFixture 为递归数据结构创建夹具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37584929/

相关文章:

c# - 使用 MySqlDataReader 读取

c# - 加入两个不同长度的列表

c# - WMI 重启远程机器

c# - 如何从查询导入组合框中剪切字符串

ios - 将单元测试移动到 xcode 中的不同文件夹

unit-testing - 保持通过单元测试有什么意义?

unit-testing - 属性测试用于什么

c# - 如何转换日期时间?到日期时间

.net - Entity Framework POCO 默认构造函数

c# - 检查功能参数的最佳方法 : Check for null or try/catch