2012-04-09 65 views
3

我想檢查我的函數返回Some(x)斷言一些與FsUnit

testedFunc() |> should be (sameAs Some) 
testedFunc() |> should be Some 
testedFunc() |> should equal Some 

所有不起作用。我寧可不使用:

match testedFunc() with 
    | Some -> Pass() 
    | None -> Fail() 

任何人都知道一種方法來做到這一點?

+0

對於使用引文結束(http://code.google.com/p/unquote/)的對比,看看https://gist.github.com/2366849 – 2012-04-12 12:12:58

回答

4

我還沒有真正使用FsUnit,但這樣的事情應該工作...

testedFunc() |> Option.isSome |> should be true 

還是因爲一個選項已經有一個IsSome屬性,你可以這樣做,但要小心情況下 - 這是不同於Option.isSome功能。

testedFunc().IsSome |> should be true 

第三種方法是組合在一起,你和Option.isSome測試,以獲得一個直接返回boolean值的函數的函數。在這個例子中,這不是很有用,但是如果你需要用多種輸入測試一個選項返回函數幾次,這種方法可以幫助減少重複的代碼。

let testedFunc = testedFunc >> Option.isSome 
testedFunc() |> should be true 
+0

現在似乎是顯而易見的,哈哈 – 2012-04-09 18:43:55