Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm testing: how to check if a Result is an Err?

Tags:

decode

elm

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?

like image 381
Richard Haven Avatar asked Dec 19 '25 13:12

Richard Haven


1 Answers

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).

like image 180
Ryan Plant Avatar answered Dec 21 '25 08:12

Ryan Plant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!