简体   繁体   English

简化 HSpec 中的不完整断言

[英]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.我想对返回复杂嵌套数据结构的 function 进行单元测试,但我只对该结构的某些字段感兴趣。 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?是否有一种通用的方法可以在 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 ;我找到了这个答案,它需要将所有无关字段从result复制到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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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