如何使用 F# 中的 xUnit 和 FsCheck 检查方法是否抛出异常

How to check if an Exception is thrown by a method with xUnit and FsCheck in F#

我正在使用 xUnit 和 FsCheck 在 F# 中编写测试,并在 C# 中执行 Diamond Kata,在尝试检查是否因用户输入无效而抛出异常时遇到了一些麻烦 (任何不是没有任何变音符号的字母的字符)。下面是代码现在的样子:

正在测试的方法:

public static string Make(char letter)
{
    if (!Regex.IsMatch(letter.ToString(), @"[a-zA-Z]"))
    {
        throw new InvalidOperationException();
    }

    // code that makes the diamond
}

测试:

[<Property>]
let ``Diamond.Make must throw an InvalidOperationException if a character that isn't 
      an alphabet letter without any diacritics is given`` (letter : char) =
    (not (('A' <= letter && letter <= 'Z') || ('a' <= letter && letter <= 'z'))) ==> lazy 
        (Assert.Throws<InvalidOperationException>(fun () -> Diamond.Make letter |> ignore))

我的方法的问题是测试表明没有抛出异常,但是当我 运行 应用程序使用测试套件显示的输入时,会引发异常。

这是测试套件给出的消息(我故意省略了测试名称和堆栈跟踪):

Test Outcome:   Failed
Test Duration:  0:00:00,066
Result Message: 
FsCheck.Xunit.PropertyFailedException : 
Falsifiable, after 1 test (0 shrinks) (StdGen (1154779780,296216747)):
Original:
')'
---- Assert.Throws() Failure
Expected: typeof(System.InvalidOperationException)
Actual:   (No exception was thrown)

虽然测试套件说对于值 ')' 没有抛出异常,但我用它进行了手动测试并且确实抛出了预期的异常。

如何确保异常被测试捕获?

我认为问题在于 Assert.Throws return 给定类型的异常(如果发生)。忽略 Assert.Throws 的 return 值应该对你有帮助。

let test (letter : char) =
    (not (('A' <= letter && letter <= 'Z') || ('a' <= letter && letter <= 'z'))) ==> 

    lazy
        Assert.Throws<InvalidOperationException>(fun () -> Diamond.Make letter |> ignore)
        |> ignore