F# FsUnit断言异常消息
如何在FsUnit中断言异常消息?NUnit的类似内容:F# FsUnit断言异常消息,f#,fsunit,F#,Fsunit,如何在FsUnit中断言异常消息?NUnit的类似内容: [<Test>] let ShouldThrowExceptionAndLog() = Assert.That ((fun () -> Calculator.Calculate "-1"), Throws.Exception.With.Property("Message").EqualTo("Negatives not allowed: -1")) [] let shoul
[<Test>]
let ShouldThrowExceptionAndLog() =
Assert.That
((fun () -> Calculator.Calculate "-1"),
Throws.Exception.With.Property("Message").EqualTo("Negatives not allowed: -1"))
[]
let shouldThroweExceptionAndLog()=
断言
((fun()->Calculator.Calculate“-1”),
抛出.Exception.With.Property(“消息”).EqualTo(“不允许否定:-1”))
编辑:我不关心异常本身,我想测试异常消息。AFAIK没有现成的匹配器来执行您想要的操作,但是自己创建一个非常简单:
open NHamcrest
let throwAnyWithMessage m =
CustomMatcher<obj>(m,
fun f -> match f with
| :? (unit -> unit) as testFunc ->
try
testFunc()
false
with
| ex -> ex.Message = m
| _ -> false )
打开NHamcrest
让我带着信息来=
客户匹配器(m,
乐趣f->与f匹配
|:?(单位->单位)作为测试函数->
尝试
testFunc()
假的
具有
|ex->ex.Message=m
|_u->false)
用法:
(fun () -> raise <| Exception("Foo") |> ignore) |> should throwAnyWithMessage "Foo" // pass
(fun () -> raise <| Exception("Bar") |> ignore) |> should throwAnyWithMessage "Foo" // fail
(fun()->raise ignore)|>应该通过带有消息“Foo”//pass的wanywhen
(fun()->raise ignore)|>应通过带有消息“Foo”//fail的WANY
AFAIK没有现成的匹配器可以满足您的需要,但您可以很容易地自己创建一个匹配器:
open NHamcrest
let throwAnyWithMessage m =
CustomMatcher<obj>(m,
fun f -> match f with
| :? (unit -> unit) as testFunc ->
try
testFunc()
false
with
| ex -> ex.Message = m
| _ -> false )
打开NHamcrest
让我带着信息来=
客户匹配器(m,
乐趣f->与f匹配
|:?(单位->单位)作为测试函数->
尝试
testFunc()
假的
具有
|ex->ex.Message=m
|_u->false)
用法:
(fun () -> raise <| Exception("Foo") |> ignore) |> should throwAnyWithMessage "Foo" // pass
(fun () -> raise <| Exception("Bar") |> ignore) |> should throwAnyWithMessage "Foo" // fail
(fun()->raise ignore)|>应该通过带有消息“Foo”//pass的wanywhen
(fun()->raise ignore)|>应通过带有消息“Foo”//fail的WANY
AFAIK没有现成的匹配器可以满足您的需要,但您可以很容易地自己创建一个匹配器:
open NHamcrest
let throwAnyWithMessage m =
CustomMatcher<obj>(m,
fun f -> match f with
| :? (unit -> unit) as testFunc ->
try
testFunc()
false
with
| ex -> ex.Message = m
| _ -> false )
打开NHamcrest
让我带着信息来=
客户匹配器(m,
乐趣f->与f匹配
|:?(单位->单位)作为测试函数->
尝试
testFunc()
假的
具有
|ex->ex.Message=m
|_u->false)
用法:
(fun () -> raise <| Exception("Foo") |> ignore) |> should throwAnyWithMessage "Foo" // pass
(fun () -> raise <| Exception("Bar") |> ignore) |> should throwAnyWithMessage "Foo" // fail
(fun()->raise ignore)|>应该通过带有消息“Foo”//pass的wanywhen
(fun()->raise ignore)|>应通过带有消息“Foo”//fail的WANY
AFAIK没有现成的匹配器可以满足您的需要,但您可以很容易地自己创建一个匹配器:
open NHamcrest
let throwAnyWithMessage m =
CustomMatcher<obj>(m,
fun f -> match f with
| :? (unit -> unit) as testFunc ->
try
testFunc()
false
with
| ex -> ex.Message = m
| _ -> false )
打开NHamcrest
让我带着信息来=
客户匹配器(m,
乐趣f->与f匹配
|:?(单位->单位)作为测试函数->
尝试
testFunc()
假的
具有
|ex->ex.Message=m
|_u->false)
用法:
(fun () -> raise <| Exception("Foo") |> ignore) |> should throwAnyWithMessage "Foo" // pass
(fun () -> raise <| Exception("Bar") |> ignore) |> should throwAnyWithMessage "Foo" // fail
(fun()->raise ignore)|>应该通过带有消息“Foo”//pass的wanywhen
(fun()->raise ignore)|>应通过带有消息“Foo”//fail的WANY
可能重复的我认为相关问题只是检查是否引发了特定的异常-不是它包含指定的消息…可能重复的我认为相关问题只是检查是否引发了特定的异常-不是它包含指定的消息…可能重复的我认为相关问题问题只是检查是否抛出了一个特定的异常-不是它包含指定的消息…我认为相关问题可能是重复的-只是检查是否抛出了一个特定的异常-不是它包含指定的消息。。。