拥有
type Category(name : string, categoryType : CategoryType) =
do
if (name.Length = 0) then
invalidArg "name" "name is empty"我试图使用FsUnit +xUnit测试这个异常:
[<Fact>]
let ``name should not be empty``() =
(fun () -> Category(String.Empty, CategoryType.Terminal)) |> should throw typeof<ArgumentException>但是当它运行时,我看到了XUnit.MatchException。我做错什么了?
发布于 2014-04-13 12:36:43
虽然我不是FsUnit专家,但我认为MatchException类型是应该的,因为FsUnit使用自定义匹配器,并且匹配没有成功。
然而,编写的测试似乎是不正确的,因为
(fun () -> Category(String.Empty, CategoryType.Terminal)是一个带有签名unit -> Category的函数,但您并不真正关心返回的Category。
相反,你可以把它写成
[<Fact>]
let ``name should not be empty``() =
(fun () -> Category(String.Empty, CategoryType.Terminal) |> ignore)
|> should throw typeof<ArgumentException>注意添加的ignore关键字,它忽略了Category返回值。此测试通过,如果删除“警卫”条款,则失败。
https://stackoverflow.com/questions/23042547
复制相似问题