Question

I'm writing unit tests (in VS2010) for a DataGridViewCell class that looks a bit like this:

    public class MyCell : DataGridViewCell
    {
        protected override object GetFormattedValue(object value, int rowIndex, ref System.Windows.Forms.DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, System.Windows.Forms.DataGridViewDataErrorContexts context)
        {
            return MyCustomFormatting(value);
        }

        private object MyCustomFormatting(object value)
        {
            var formattedValue = string.Empty;
            // ... logic to test here
            return FormattedValue;
        }
    }

I want to test the public property .FormattedValue is set correctly. However this always returns null if the cell being tested has no DataGridView set (I checked this with Telerik's JustDecompile refection tool).

I can obviously bypass this and just use accessors to access the protected or private methods, but accessors are deprecated in VS2012 onwards.

How can I unit test this logic without using accessors?

Était-ce utile?

La solution

Since obviously there is a lot to be done to set up the DataGridViewCell's and DataGridView's, why don't you try something else?

First, create a component that does your special formatting:

public class SpecialFormatter
{
  public object Format(object value)
  {
    var formattedValue = string.Empty;
    // ... logic to test here
    return FormattedValue;
  }
}

And then use it your DataGridViewCell implementation:

public class MyCell : DataGridViewCell
{
  protected override object GetFormattedValue(object value, int rowIndex, ref System.Windows.Forms.DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, System.Windows.Forms.DataGridViewDataErrorContexts context)
  {
    return MyCustomFormatting(value);
  }

  private object MyCustomFormatting(object value)
  {
    return new SpecialFormatter.Format(value);
  }
}

Then you go ahead and unit test SpecialFormatter and you're done. Unless there is much else going on in your DataGridViewCell implementation, then there is not much value in testing it.

Autres conseils

I did find this info on MSDN Upgrading Unit Tests from Visual Studio 2010 where it explains about using PrivateObject if melike's answer is not an option for some reason.

Even though melike's answer worked for me, I thought I'd just learn how this works, here's a sample test in case it helps anyone:

    [TestMethod]
    public void MyCellExampleTest()
    {
        var target = new MyCell();
        var targetPrivateObject = new PrivateObject(target);

        var result = targetPrivateObject.Invoke("MyCustomFormatting", new object[] { "Test" });

        Assert.AreEqual(string.Empty, result);
    }

One obvious downside of using the PrivateObject is the method name is just stored as a string - will be harder to maintain if you change the name or method signature.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top