2012-02-25 3 views
2

私はF#とXunitを使用しています。私はXunitのAssert.Equal()を使用すると、比較対象の型が文字列の場合に"<string>"を指定する必要があることがわかりました。例えばF#は、Xunitを使用して文字列に同等性をアサートするときにタイプ情報が必要です

この実行のとコンパイル:

[<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'. 
+0

関連:http://stackoverflow.com/questions/5667372/what-unit-testing-frameworks-are-available-for-f/5669263#5669263(つまり、劇的に過小に公表されている慣用的なF#アサーション) –

答えて

4

stringは、第一および第二オーバーロード(覚えている:string :> seq<char>を)の両方で一致させることができるためだこと。私が期待するよう@Ramon SNIRが指摘するように、オーバーロード解決アルゴリズムが供給stringタイプはよりstringに「近い」であることを認識することによって曖昧さを解決するstring :> seq<char><string>

4

あなたの例では、(私のために、エラーなしで実行を削除しましたseq<char>)。

[<Fact>] 
let Test_XunitStringAssertion() = 
    let s1 = "Stuff" 
    Assert.Equal("Stuff",s1) 

あなたが提供したサンプルは、問題を引き起こしている実際のコードとまったく同じではないと思います。たぶん実際のコードのs1は実際にはstringではない(または少なくともコンパイラはそれがわからない)。

関連する問題