某些测试断言框架允许您向断言添加自定义消息,例如使用 NUnit 的以下内容:
Assert.AreEqual(1, result, "the result should be one")
Run Code Online (Sandbox Code Playgroud)
在 F# 中使用Unquote时是否可以执行相同的操作?
test <@ result = 1 @>
Run Code Online (Sandbox Code Playgroud)
更新
我最接近的是在引用的表达式中添加一个简单的注释。由于我这样做的动机是记录正在验证的内容(我倾向于不止一次地断言!),这足以满足我的需求。
test <@
// the result should be one
result = 1
@>
Run Code Online (Sandbox Code Playgroud)
另一个更新
我一直在使用 Stephen 的建议ignore "description here";,我真的很喜欢。我发现如果我像这样声明自己的函数会更容易阅读:
> let inline checking _ = ()
> let result = 2;;
> test <@ checking "the result should be one"; result = 1 @>;;
Test failed:
checking "the result should be one"; result = 1
(); result = 1
result = 1
2 = 1
false
Run Code Online (Sandbox Code Playgroud)
你可以做这样的事情(使用 F# Interactive)
> let result = 2;;
> test <@ ignore "the result should be one"; result = 1 @>;;
Test failed:
ignore "the result should be one"; result = 1
(); result = 1
result = 1
2 = 1
false
Run Code Online (Sandbox Code Playgroud)
但是,如果您使用像expectedand这样的变量名称actual,Unquote 的输出可能足够丰富,足以消除对此类补充注释的需要
> let actual = 2;;
> let expected = 1;;
> test <@ actual = expected @>;;
Test failed:
actual = expected
2 = 1
false
Run Code Online (Sandbox Code Playgroud)