I am trying to write my first generic function in F#, and it looks like I am missing some concept here :
module Assert =
let ThrowsException<'e : exn> functionUnderTest =
try
let result = functionUnderTest
printfn "fails"
with
| :? 'e -> printfn "succeeds"
| _ -> printfn "fails"
stdin(18,28): error FS0010: Unexpected symbol ‘:’ in pattern. Expected ‘>’ or other token.
I would like to be able to use my function like that to test if an exception was raised.
let myFunction=HttpClient.postDocRaw "http://notexisting.com/post.php" "hello=data"
Assert.ThrowsException System.Net.WebException myFunction
I would like to achieve this tool for testing so if anybody has a solution , that’s nice, but furthermore, I am learning F#, and I would like to know where I go wrong trying to write functions like this.
final code :
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")
The error says “Expected ‘>'” because a type constraint takes the form
'T when 'T :> baseType. Your code should be:See Constraints on MSDN for more info.