[英]Comparing SqlDataType to CLR nullable types in nunit
我正在編寫測試來驗證一個映射操作,該操作采用一個充滿SqlDataTypes的胖的重型數據對象,並使用簡單的CLR值類型將它們轉換為pocos。
這是我迄今為止構建的方法:
private static void CompareValues(string k, Dictionary<string, string> propertyMap, TData sourceDal, TEntity entity)
{
string sourceField = k;
string destField = propertyMap[k];
object sourceval = sourceDal.GetType().GetProperty(sourceField).GetValue(sourceDal, null);
object destval = entity.GetType().GetProperty(destField).GetValue(entity, null);
Assert.AreEqual(sourceval,
destval,
String.Format("Values not equal on fields {0} ({1}) to {2} ({3})",
sourceDal.GetType().GetProperty(sourceField).Name, sourceDal.GetType().GetProperty(sourceField).PropertyType,
entity.GetType().GetProperty(destField).Name, entity.GetType().GetProperty(destField).PropertyType)
);
}
不幸的是,在將SqlInt32與int?進行比較時,此方法無法通過測試。 sourceval的“值”顯示為{74}
(具有通常的額外SqlDataType屬性的復雜類型),而destval的值顯示為74
。
我不想讓這個方法被類型識別 - 我不希望它假設一方是sql類型而另一方不是 - 因為我的映射測試將在兩個方向上傳遞數據。
我試過在SqlInt32上擴展CompareTo
public static int CompareTo(this SqlInt32 sqlInt32, int? value)
public static int CompareTo(this SqlInt32 sqlInt32, int value)
但是沒有調用擴展方法(盡管intelisense在我的測試項目中檢測到它們)
我吠叫錯了樹嗎? 如何在SqlDataTypes和CLR值類型之間設置一般比較,以產生准確的結果?
很粗糙,根本不喜歡它,但結果是將vals轉換為字符串作品。
但仍然可以提供更好的建議。
private static void CompareValues(string k, Dictionary<string, string> propertyMap, TData sourceDal, TEntity entity)
{
string sourceField = k;
string destField = propertyMap[k];
object sourceval = sourceDal.GetType().GetProperty(sourceField).GetValue(sourceDal, null);
string sSource = sourceval.ToString();
object destval = entity.GetType().GetProperty(destField).GetValue(entity, null);
string sDest = destval.ToString();
Assert.AreEqual(sSource,
sDest,
String.Format("Values not equal on fields {0} ({1}) to {2} ({3})",
sourceDal.GetType().GetProperty(sourceField).Name, sourceDal.GetType().GetProperty(sourceField).PropertyType,
entity.GetType().GetProperty(destField).Name, entity.GetType().GetProperty(destField).PropertyType)
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.