1
我試圖寫在F#我的第一個泛型函數,它看起來像我在這裏缺少一些概念:宣佈在一個通用的函數f#
module Assert =
let ThrowsException<'e : exn> functionUnderTest =
try
let result = functionUnderTest
printfn "fails"
with
| :? 'e -> printfn "succeeds"
| _ -> printfn "fails"
標準輸入(18,28):錯誤FS0010:模式中意外的符號':'。預期的'>'或其他標記。
我希望能夠使用我的函數來測試是否引發異常。
let myFunction=HttpClient.postDocRaw "http://notexisting.com/post.php" "hello=data"
Assert.ThrowsException System.Net.WebException myFunction
我想因此,如果任何人有一個解決方案,這很好,但此外,我學習F#實現這一工具進行測試,我想知道我走到哪裏錯了嘗試寫這樣的功能。
最終代碼:
module Assert =
let ThrowsException<'e when 'e :> exn> functionUnderTest =
try
let actual = functionUnderTest()
printfn "succeeds"
with
| :? 'e -> printfn "succeeds"
| _ -> Assert.Fail()
[<TestClass>]
type HttpClientTest() =
[<TestMethod>]
member x.PostDataWrongUrl() =
Assert.ThrowsException<System.Net.WebException> (fun() -> HttpClient.postDocRaw "http://notexisting.com/post.php" "hello=data")
太棒了!非常感謝,我正在玩弄時間和地點:>但由於某種原因,從未按此順序。我也會看看這個鏈接。 – Arthis