Simplify incomplete assertions in HSpec

69 views Asked by At

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?

1

There are 1 answers

0
Ari Fordsham On BEST ANSWER

A simple approach:

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