Search code examples
f#fsunit

Assert Some with FsUnit


I am trying to check that my function returns Some(x)

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

All don't work. I'd rather not use:

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

Anyone know of a way to do this?


Solution

  • I haven't really used FsUnit, but something like this should work...

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

    Or because an Option already has an IsSome property, you could do this, but be careful of case - it's different from the Option.isSome function.

    testedFunc().IsSome |> should equal true
    

    A third approach would be to compose together the function you're testing with Option.isSome to get a function that returns boolean directly. This isn't so useful in this example, but if you need to test an Option-returning function several times with a variety of inputs, this approach could help reduce duplicate code.

    let testedFunc = testedFunc >> Option.isSome
    testedFunc() |> should equal true