[英]Write nested assertions with Hspec
Ihm struggling to implement a seemingly simple testing problem with HSpec: I'd like to test a function我正在努力用 HSpec 实现一个看似简单的测试问题:我想测试一个 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.在一个测试用例中,我想首先断言返回值是Right
值,然后打开该值以断言其内容。 I haven't figured out a way to do this without cumbersome case expressions.如果没有繁琐的 case 表达式,我还没有想出一种方法来做到这一点。 Is there an assertion operator that simultaneously allows for pattern matching?是否有同时允许模式匹配的断言运算符?
What I am looking for is something along the following lines (pseudo-Haskell):我正在寻找的是以下几行(伪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.这只是为了说明这个想法,当然,RHS 上的模式匹配不起作用。
Use a custom function with shouldSatisfy
, like this: result `shouldSatisfy` either (const False) (<expectedRecord> ==)
将自定义 function 与shouldSatisfy
一起使用,如下所示: result `shouldSatisfy` either (const False) (<expectedRecord> ==)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.