FsUnit で例外を適切にテストする方法を見つけようとしています。公式ドキュメントには、例外をテストするには、次のようなものを修正する必要があると記載されています。
(fun () -> failwith "BOOM!" |> ignore) |> should throw typeof<System.Exception>
[<ExpectedException>]
しかし、テスト メソッドを属性でマークしないと、常に失敗します。例外をテストしたい場合は、C# + NUnit でそのような属性を追加する必要があるため、合理的に聞こえます。
ただし、この属性を追加している限り、スローしようとしている例外の種類は問題ではなく、常に処理されます。
いくつかのスニペット: My LogicModule.fs
exception EmptyStringException of string
let getNumber str =
if str = "" then raise (EmptyStringException("Can not extract number from empty string"))
else int str
私の LogicModuleTest.fs
[<Test>]
[<ExpectedException>]
let``check exception``()=
(getNumber "") |> should throw typeof<LogicModule.EmptyStringException>