FsUnit“应等于"在“某些[]"上失败 [英] FsUnit `should equal` fails on `Some []`
问题描述
当我使用NUnit 2.6.3运行此FsUnit测试时,
When I run this FsUnit test with NUnit 2.6.3,
let f xs = Some (List.map ((+) 2) xs)
[<Test>]
let test() =
f []
|> should equal (Some [])
我得到:
Result Message:
Expected: <Some([])>
But was: <Some([])>
Result StackTrace:
at FsUnit.TopLevelOperators.should[a,a](FSharpFunc`2 f, a x, Object y)
即使消息中的期望值"和实际值"相同,测试也会失败.发生什么事了?
The test fails even though the Expected and Actual in the message are the same. What happened?
推荐答案
原因是FsUnit在后台使用了无类型机制,因此类型检查器将Expected
推断为object
(请参见堆栈跟踪).
The reason is that FsUnit uses untyped mechanism under the hood so Expected
is inferred as object
by the type checker (see the Object y
part in the stacktrace).
一种解决方法是为通用值添加类型注释,即
A workaround is to add type annotation for generic values i.e.
[<Test>]
let test() =
f []
|> should equal (Some ([]: int list))
几个人被这种东西咬了. 类型提供程序中的奇怪的无"行为.
Several people have been bitten by this e.g. Weird None behaviour in type providers.
流利的断言之美对我来说毫无意义,一旦它们不再是类型安全的.我建议创建一个类型安全替代:
Beauty of fluent assertions is pointless to me once they're no longer type-safe. I suggest to create a type-safe alternative:
let shouldEqual (x: 'a) (y: 'a) =
Assert.AreEqual(x, y, sprintf "Expected: %A\nActual: %A" x y)
这篇关于FsUnit“应等于"在“某些[]"上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!