c# - 将 SqlDataType 与 nunit 中的 CLR 可空类型进行比较

标签 c# nunit

我正在编写测试来验证一个映射操作,该操作采用一个充满 SqlDataType 的胖重数据对象,并使用简单的 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)
            );

    }

关于c# - 将 SqlDataType 与 nunit 中的 CLR 可空类型进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9177873/

相关文章:

c# - 在类中并行运行 NUnit 测试但不与其他类测试一起运行的方法?

xml - NUnit 是否有 XML 断言?

c# - 如何从 javascript 填充文本框值并执行剩余代码

c# - 如何将接口(interface) obj 转换为类 obj?

unit-testing - 强制 nunit 控制台运行程序使用 CLR 4.5

.net - 确定程序集目录时 NUnit 出现问题

c# - SpecFlow - Visual Studio 的 HTML 输出

c# - 打开路径中包含逗号的文件夹

c# - 如何在 C# 项目中使用 F# 定义的类型

c# - 如何在C# winform中一行显示listview中的文件?