Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# FsUnit断言异常消息_F#_Fsunit - Fatal编程技术网

F# FsUnit断言异常消息

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

如何在FsUnit中断言异常消息?NUnit的类似内容:

[<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

可能重复的我认为相关问题只是检查是否引发了特定的异常-不是它包含指定的消息…可能重复的我认为相关问题只是检查是否引发了特定的异常-不是它包含指定的消息…可能重复的我认为相关问题问题只是检查是否抛出了一个特定的异常-不是它包含指定的消息…我认为相关问题可能是重复的-只是检查是否抛出了一个特定的异常-不是它包含指定的消息。。。