2012-02-07 61 views
3

我正在編寫測試來驗證一個映射操作,該操作將胖滿數據對象充滿SqlDataTypes並使用簡單的CLR值類型將它們轉換爲pocos。將SqlDataType與nunit中的CLR可空類型進行比較

這是迄今爲止我已經建立了方法:

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) 

但擴展方法不叫(即使智能感知並在我的測試項目中正確地檢測它們)

難道我叫錯樹?如何設置SqlDataTypes和CLR值類型之間的通用比較以獲得準確的結果?

回答

0

非常粗糙,根本不喜歡它,但結果將vals轉換爲string作品。

儘管如此,仍然可以提供更好的建議。

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) 
      ); 

    } 
相關問題