【问题标题】:F# unit tests and assertion on pattern matchingF# 单元测试和关于模式匹配的断言
【发布时间】:2018-03-12 22:59:19
【问题描述】:

我开始在 F# 中做保龄球 kata (http://codingdojo.org/kata/Bowling/)。我写了第一个单元测试:

[<Fact>]
let ``If no roll was made then the current frame should be the first one`` () =
    let game = newGame()
    let cf = currentFrame game
    match cf with
    | TenthFrame _ -> Assert.True(false)
    | Frame frame ->
        let (firstFrames, _) = deconstructGame game
        Assert.Equal (frame, List.item 0 firstFrames)

测试通过了,但是“Assert.True(false)”部分对我来说很难看...有没有更好的写法?

【问题讨论】:

  • xunit docs 好像没有Assert.Fail () 方法。您的解决方案似乎很好。
  • 好的,谢谢你的链接。我期待一种更优雅或更实用的方式来编写这样的代码。将此作为答案发布,我会接受。
  • Expecto 拥有isFalse
  • 您可以定义一个名称稍微好一点的函数:let failTest msg = Assert.True(false, msg),然后在您的测试用例中使用它:match cf with | TenthFrame _ -&gt; failTest "Should be on first frame, not tenth"

标签: f# xunit


【解决方案1】:

来自docs。 xunit 没有提供像Assert.Fail () 这样的方法。建议使用Assert.True (false, "message"),就像您正在做的那样。

【讨论】:

  • 我也会试试Expecto,感谢@s952163
猜你喜欢
  • 2014-08-06
  • 1970-01-01
  • 2021-02-21
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多