简体   繁体   中英

Write nested assertions with Hspec

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM