我目前正在使用 scalaz 中的 |>
来实现以下目的:
(4 |> (s => (s + " is smaller than 10" ! (s < 10 must beTrue)))
这允许我在测试用例的描述中重用对象的 .toString
。但由于 Specs2 似乎支持某种复杂的东西,例如通过扫描 src 文件夹从测试源中提取内容——他们一定考虑过这个特定的用例。
还有 Given
、When
、Then
等内容,但看起来非常冗长。
那么如何重用同一个对象来进行描述和匹配器呢?我认为我的解决方案是可以接受的,但括号困扰着我。实际上它看起来像这样:
(Set((1,2),(3,4)) |> (s => s + " has Set(1,2) as component of 1" ! (graph.componentOf(s,1) must_== Set(1,2))))
I'm currently using |>
from scalaz for achieving the following:
(4 |> (s => (s + " is smaller than 10" ! (s < 10 must beTrue)))
This allows me to reuse the .toString
of an object inside the description of the test case. But since Specs2 seems to support kind of complicated stuff like extracting things from the source of the tests by scanning the src
folder --- they must have thought about this particular use case.
There's also the Given
, When
, Then
stuff but it looks extremely verbose.
So how do I reuse the same object for description and matcher? I think my solution is acceptable but the parenthesis are bugging me. In reality it looks like this:
(Set((1,2),(3,4)) |> (s => s + " has Set(1,2) as component of 1" ! (graph.componentOf(s,1) must_== Set(1,2))))
发布评论
评论(1)
当示例的正文非常接近描述时,您可以使用“auto-examples":
在这种情况下,使用 sbt 执行时,示例的主体将直接用作描述:
请注意,我将期望写为
4 必须be_<(10)
而不是(4 < 10) 必须为True
,因为如果出现问题,失败消息会更加明确:如果您想使用以下命令增强失败消息 :甚至更多信息,您还可以使用
aka
运算符:最后,还有一个更简单版本的 Give-When-Then,您可以在其中简单地 重用示例的描述:
When the body of the example is very close to the description you can just use "auto-examples":
In that case, the body of the example is directly used as a description when executed with sbt:
Note that I wrote the expectations as
4 must be_<(10)
instead of(4 < 10) must beTrue
because the failure message is going to be more explicit if something goes wrong:If you want to enhance a failure message with even more information you can also use the
aka
operator:Finally, there is a simpler version of Given-When-Then, where you can simply reuse the description of an example: