如何在IDataErrorInfo业务对象中单元测试错误?

Bob*_*Bob 2 wpf unit-testing idataerrorinfo

我正在为WPF应用程序编写(尝试编写)单元测试.

UI绑定的业务对象实现IDataErrorInfo,这样当我在View xaml中设置ValidatesOnDataErrors = True时,只要调用绑定业务对象的setter,就会调用错误索引器(this []).那部分很棒.

现在,如果我从unitTest调用同一属性的setter,它永远不会调用错误索引器.如何强制在单元测试中评估IDataErrorInfo索引器?

仅用于说明,这是我的一个包含Name属性的简单错误索引器.设置'myObject.Name = string.Empty;' 当我在单元测试中执行此操作时,会调用setter,但不会调用错误索引器.

        public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            IsDirty = true;
            OnPropertyChanged("Name");
        }
    }

        #region IDataErrorInfo

    public Dictionary<string, string> ErrorCollection;

    public string this[string property]
    {
        get
        {
            string msg = null;
            switch (property)
            {
                case "Name":
                    if (string.IsNullOrEmpty(Name))
                        msg = "ICU Name is required.";
                    else if (Name.Length < 4)
                        msg = "ICU Name must contain at least 4 characters.";
                    else if (_parent.Units.AsEnumerable().Count(u => u.Name == Name) > 1)
                        msg = Name + " already exists, please change to a different Name.";
                    break;
            }


            if (msg != null && !ErrorCollection.ContainsKey(property))
                ErrorCollection.Add(property, msg);
            if (msg == null && ErrorCollection.ContainsKey(property))
                ErrorCollection.Remove(property);

            return msg;
        }
    }

    public string Error
    {
        get { return null; }
    }
    #endregion
Run Code Online (Sandbox Code Playgroud)

谢谢!

Joe*_*ant 6

您需要自己挂钩PropertyChanged事件,然后在调用处理程序时,使用属性名称调用索引器.或者不要挂钩事件并使用您正在测试的属性的名称调用索引器.

这就是.NET的作用.它使用属性名称调用索引器.

MyClass mc = new MyClass();
mc.Name = "abc";
string error = mc["Name"];
Run Code Online (Sandbox Code Playgroud)