我是f#和fsUnit的新手,我想知道如何使用fsUnit测试模式匹配语句。例如,如果我有以下代码,您将如何为其编写fsunit测试?
let Menu () =
let Choice = Console.ReadLine()
match Choice with
| "A" | "a" -> Function1()
| "B" | "b" -> Function2()
| "C" | "c" -> Function3()
| _ -> Printfn"Error"发布于 2017-12-23 06:58:56
首先,您应该将实现匹配逻辑的代码与读取输入的代码分开,因为您只能测试某些调用的结果是否正确:
let handleInput choice =
match choice with
| "A" | "a" -> Function1()
| "B" | "b" -> Function2()
| "C" | "c" -> Function3()
| _ -> "Error"
let menu () =
let choice = Console.ReadLine()
let output = handleInput choice
printfn "%s" output现在,您可以编写一系列测试来检查handleInput返回的字符串是否为您期望用于每个输入的字符串:
handleInput "A" |> should equal "whatever Function 1 returns"
handleInput "b" |> should equal "whatever Function 2 returns"
handleInput "D" |> should equal "Error"https://stackoverflow.com/questions/47946136
复制相似问题