1

I'm writing tests for functions that return a Result. How do I test that it "is" an Err (or an Ok, for that matter) ?

\() -> Expect.equal expectedFailure (Err _)

does not work.

How does one decode a non-parameter?

Richard Haven
  • 1,122
  • 16
  • 31

1 Answers1

5

There may well be a more elegant solution I've missed, but I personally would just write a helper function.

resultOk result =
    case result of
        Ok _ -> True
        Err _ -> False

then in your tests

Expect.true "expected this to be OK" (resultOk <| Ok "All good")
Expect.false "expected this to be an error" (resultOk <| Err "Oh no!")

Expect.true and Expect.false take a string to print if the test fails, and then an expression that should be true (in the case of Expect.true) or false (in the case of Expect.false).

Ryan Plant
  • 1,037
  • 1
  • 11
  • 18
  • I wondered if I could decode the value inside the function if it was not a parameter. I think your way is more direct and I am creating helper functions like this. Thanks – Richard Haven Jan 02 '17 at 05:18