Write nested assertions with Hspec

219 views Asked by At

Ihm struggling to implement a seemingly simple testing problem with HSpec: I'd like to test a function

myFunc :: (Exception e) a -> Either e MyRecord

In one test case, I'd like to first assert that the return value is a Right value, then unwrap the value to assert its contents. I haven't figured out a way to do this without cumbersome case expressions. Is there an assertion operator that simultaneously allows for pattern matching?

What I am looking for is something along the following lines (pseudo-Haskell):

describe "myFunc" $ do
    it "should return funky stuff" $ do
      let result = myFunc <testArgument>
      result `shouldBe` (Right testRecord)
      testRecord `shouldBe` <expectedRecord>

This is just to illustrate the idea, pattern matching on the RHS doesn't work, of course.

1

There are 1 answers

0
Joseph Sible-Reinstate Monica On

Use a custom function with shouldSatisfy, like this: result `shouldSatisfy` either (const False) (<expectedRecord> ==)