使用 FsCheck 属性 属性时如何排除空值?

How to exclude null value when using FsCheck Property attribute?

我需要编写一个简单的方法来接收参数(例如 string)并执行 smth。通常我会进行两次测试。第一个是 guard clause。第二个将验证 expected 行为(为简单起见,该方法不应失败):

[Fact]
public void DoSmth_WithNull_Throws()
{
    var sut = new Sut();
    Assert.Throws<ArgumentNullException>(() =>
        sut.DoSmth(null));
}

[Fact]
public void DoSmth_WithValidString_DoesNotThrow()
{
    var s = "123";
    var sut = new Sut();
    sut.DoSmth(s); // does not throw
}

public class Sut
{
    public void DoSmth(string s)
    {
        if (s == null)
            throw new ArgumentNullException();

        // do smth important here
    }
}

当我尝试利用 FsCheck [Property] 属性生成随机数据时,null 和许多其他随机值被传递给测试,这在某些时候会导致 NRE:

[Property]
public void DoSmth_WithValidString_DoesNotThrow(string s)
{
    var sut = new Sut();
    sut.DoSmth(s); // throws ArgumentNullException after 'x' tests
}

我意识到这就是 FsCheck 的全部思想,生成大量随机数据以涵盖不同的情况,这绝对很棒。

是否有任何优雅的方法来配置 [Property] 属性以排除 不需要的 值? (在这个特定的测试中是 null)。

FsCheck 有一些内置类型可用于指示特定行为,例如,引用类型值不应为空。其中之一是 NonNull<'a>。如果您要求其中之一,而不是要求原始字符串,您将不会得到空值。

在 F# 中,您可以将其解构为函数参数:

[<Property>]
let DoSmth_WithValidString_DoesNotThrow (NonNull s) = // s is already a string here...
    let sut = Sut ()
    sut.DoSmth s // Use your favourite assertion library here...
}

我觉得在C#里应该是这样的,不过我没试过:

[Property]
public void DoSmth_WithValidString_DoesNotThrow(NonNull<string> s)
{
    var sut = new Sut();
    sut.DoSmth(s.Get); // throws ArgumentNullException after 'x' tests
}