簡體   English   中英

如何在f#中為模式匹配創建一個fsunit測試?

[英]How to create a fsunit test for pattern matching in f#?

我是f#和fsUnit的新手,我想知道如何使用fsUnit測試模式匹配語句。 例如,如果我有以下代碼,你會如何為它編寫一個fsunit測試?

let Menu () = 
    let Choice = Console.ReadLine()

        match Choice with
        | "A" | "a" -> Function1()
        | "B" | "b" -> Function2()
        | "C" | "c" -> Function3()
        | _ ->  Printfn"Error"

首先,您將實現匹配邏輯的代碼與讀取輸入的代碼分開,因為您只能測試某些調用的結果是否正確:

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM