【发布时间】:2012-02-27 03:05:41
【问题描述】:
我正在使用 F# 和 Xunit。 (我对两者都比较陌生)
我发现当我使用 Xunit 的 Assert.Equal() 时,当被比较的类型是字符串时,我需要指定 "<string>"。
例如这个运行和编译:
[<Fact>]
let Test_XunitStringAssertion() =
let s1 = "Stuff"
Assert.Equal<string>("Stuff",s1)
我的问题是,为什么我不能删除 "<string>" 而只是断言 "Assert.Equal("Stuff",s1)" 代替?
在我看来编译器知道这两个参数的类型,所以为什么要大惊小怪?
以下是编译Assert.Equal("Stuff",s1)时返回的错误:
error FS0041: A unique overload for method 'Equal' could not be determined based on type information prior to this program point. The available overloads are shown below (or in the Error List window). A type annotation may be needed.
error FS0041: Possible overload: 'Assert.Equal<'T>(expected: 'T, actual: 'T) : unit'.
error FS0041: Possible overload: 'Assert.Equal<'T>(expected: seq<'T>, actual: seq<'T>) : unit'.
error FS0041: Possible overload: 'Assert.Equal<'T>(expected: 'T, actual: 'T, comparer: System.Collections.Generic.IEqualityComparer<'T>) : unit'.
error FS0041: Possible overload: 'Assert.Equal(expected: float, actual: float, precision: int) : unit'.
error FS0041: Possible overload: 'Assert.Equal(expected: decimal, actual: decimal, precision: int) : unit'.
error FS0041: Possible overload: 'Assert.Equal<'T>(expected: seq<'T>, actual: seq<'T>, comparer: System.Collections.Generic.IEqualityComparer<'T>) : unit'.
【问题讨论】:
-
相关:stackoverflow.com/questions/5667372/…(即,被严重低估的惯用 F# 断言)