f# - FsUnit assert exception message -


how assert on exception message in fsunit? nunit:

[<test>] let shouldthrowexceptionandlog() =      assert.that         ((fun () -> calculator.calculate "-1"),           throws.exception.with.property("message").equalto("negatives not allowed: -1")) 

edit: don't care exception itself, i'd test exception message.

afaik there no out-of-the-box matcher want, it's easy enough create 1 yourself:

open nhamcrest  let throwanywithmessage m =     custommatcher<obj>(m,         fun f -> match f                     | :? (unit -> unit) testfunc ->                         try                             testfunc()                             false                                                 | ex -> ex.message = m                     | _ -> false ) 

usage:

(fun () -> raise <| exception("foo") |> ignore) |> should throwanywithmessage "foo" // pass (fun () -> raise <| exception("bar") |> ignore) |> should throwanywithmessage "foo" // fail 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -