断言字典类型是应该存在的类型

时间:2015-05-11 09:34:58

标签: c# unit-testing dictionary

我们在许多程序中使用了一个Dll,我负责编写一些单元测试来断言它基本上没有改变,测试所有返回的对象。

我遇到的问题最多的部分是字典类型。我想断言,例如bob.newfilter是Dictionary类型的返回字典。

有问题的DLL代码返回一个过滤器对象

LinearLayout.LayoutParams lp = initial_wrapper.getLayoutParams();//will need to casting  
lp.gravity=   Gravity.RIGHT; // this is the important part
initial_wrapper.setLayoutParams(lp);

(请不要对此代码发表评论,因为它就是这样)

我尝试使用Nunit和Microsoft的测试框架尝试断言bob.GreenIsVisible是一个返回类型

的字典。
using System.Collections.Generic;
namespace Crazy.dll
{
    public class Filters
    {
        public Dictionary<int, bool> ColourIsVisible;
        public Dictionary<string, bool> AlphaFilter;
        public Dictionary<string, bool> RedIsVisible;
        public Dictionary<string, bool> BlueFilter;
        public Dictionary<string, bool> BlueIsVisible;
        public Dictionary<string, bool> GreenIsVisible;
        public Filters() // <-- returned object bit interested in testing
        {
            ColourIsVisible = new Dictionary<int, bool>();
            AlphaFilter = new Dictionary<string, bool>();
            RedIsVisible = new Dictionary<string, bool>();
            BlueFilter = new Dictionary<string, bool>();
            BlueIsVisible = new Dictionary<string, bool>();
            GreenIsVisible = new Dictionary<string, bool>();
       }
    }
}

所有人都返回了相同的信息

结果消息:Assert.AreEqual失败。 预期:     

(对我来说他们看起来一样)

任何帮助都非常感激。

2 个答案:

答案 0 :(得分:0)

您可以在此处使用Assert.IsInstanceOfType方法,因为您需要检查对象的类型,而不是实际值或实例的等同性。

答案 1 :(得分:0)

您正在尝试使用文档中的Assert<T>(T, T)

  

使用等号运算符验证两个指定的泛型类型数据是否相等。如果断言不相等,断言就会失败。

因此它不会在这里工作,因为物体自然不会相等。您应该使用Assert.IsInstanceOfType(Object, Type)来检查此内容。