首页 > 解决方案 > 如果列表小于 8,则未检测到突变

问题描述

最近我开始做一些 C#,来自 PHP。我玩语言和学习的方式是做一些 TDD。因此,我获取了一些 RFC 规范并编写了一些测试(此处为 RFC 规范)。

我现在正在使用 NUnit,并使用 Stryker 来检查测试质量。

情况如下:我有一个抽象的 Uuid 类,它有一个获取版本、时间戳字节、时钟序列字节和节点字节的构造函数。由于 RFC 确定了它们的大小,我编写了一些测试:

[Test]
public void Expects8TimestampBytes()
{
    // given an invalid count of timestamp bytes
    var timestampBytes = new List<byte>
    {
        0x00,
    };

    // when trying to create an Uuid from it
    TestDelegate instanciation = () => ConcreteUuid.Constructor2(timestamp: timestampBytes);

    // then an exception should be thrown
    Assert.That(instanciation, Throws.ArgumentException);
}

完整的测试课程可在此处获得

测试为绿色,抛出异常:

protected Uuid(int version, List<byte> timestamp, List<byte> clockSequence, List<byte> node)
{
    if (timestamp.Count != 8)
    {
        throw new ArgumentException($"Timestamp bytes count must be 8, got {timestamp.Count}");
    }
    // more code ...
}

完整课程在这里

使用 NUnit 进行的测试报告一切都很好,但使用 Stryker 的突变仍然存在,并且条件显示为红色,而 clockSequence.Count 和 node.Count 上的突变为绿色。 突变报告

问题似乎来自值 8,可能是因为分配的项目(这没有意义,因为 .Count 与 .Capacity 无关),TrimExcess() 没有解决问题。杀死突变的方法是将timestampBytes设置为9+的列表,小于8会使突变存活,我不明白为什么节点和时钟序列的突变被杀死的大小小于8(尝试重新排序条件)。

数字 8 有什么特别之处吗?

标签: c#.netnunitmutation-testing

解决方案


推荐阅读