简体   繁体   中英

Simplify incomplete assertions in HSpec

I want to unit-test a function that returns a complex nested data structure, but I'm only interested in certain fields of that structure. For example:

expectedResult = Right (
  UserRecord {
    name = "someName",
    id = <don't care>
    address = AddressRecord {
      street = "someStreet",
      id = <don't care> 
      }
    }
  )

Is there a generic way to assert a result of the above form in HSpec? That is, some way to write an expression

result `shouldBe` expectedResult

where I do not need to specify those parts of the expected result that I am not interested in? I found this answer, which requires to copy over all don't care fields from result to expectedResult ; this might get rather tedious. Maybe there is a standard approach using lenses? Or some library with assertion helpers I haven't heard of?

A simple approach:

result `shouldSatisfy` \a ->
    name a == "someName"  &&
    street (address a) == "someStreet"

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