title: How do I unit test errors in an IDataErrorInfo business object?
body: <p>I am writing (attempting to write) unit tests for a WPF application.  </p>
  
  <p>The business objects that the UI binds to implement IDataErrorInfo, such that when I set ValidatesOnDataErrors=True in my View xaml, the error indexer (this[]) is called anytime the setter for the bound business object is called.
  That part is great.</p>
  
  <p>Now if I call that same property's setter from a unitTest, it never calls the error indexer.  How do I force the IDataErrorInfo indexer to be evaluated from within a unit test?</p>
  
  <p>just for illustration, here is one of my simple error indexers that contains a Name property.  Setting 'myObject.Name = string.Empty;' does call the setter, but not the error indexer when I do this in my unit tests.</p>
  
  <pre><code>        public string Name
      {
          get { return _name; }
          set
          {
              _name = value;
              IsDirty = true;
              OnPropertyChanged("Name");
          }
      }
  
          #region IDataErrorInfo
  
      public Dictionary&lt;string, string&gt; 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 &lt; 4)
                          msg = "ICU Name must contain at least 4 characters.";
                      else if (_parent.Units.AsEnumerable().Count(u =&gt; u.Name == Name) &gt; 1)
                          msg = Name + " already exists, please change to a different Name.";
                      break;
              }
  
  
              if (msg != null &amp;&amp; !ErrorCollection.ContainsKey(property))
                  ErrorCollection.Add(property, msg);
              if (msg == null &amp;&amp; ErrorCollection.ContainsKey(property))
                  ErrorCollection.Remove(property);
  
              return msg;
          }
      }
  
      public string Error
      {
          get { return null; }
      }
      #endregion
  </code></pre>
  
  <p>Thanks!</p>