2
using System;
using System.Collections.Generic;
using System.Text;

[Serializable]
public class ColumnDataField
{
    #region Fields

    private int _columnIndex;
    private string _dataFields;

    #endregion Fields

    #region Properties

    /// <summary>
    /// Column index
    /// </summary>
    public int ColumnIndex
    {
        get { return _columnIndex; }
        set { _columnIndex = value; }
    }

    /// <summary>
    /// Data fields
    /// </summary>
    public string DataFields
    {
        get { return _dataFields; }
        set { _dataFields = value; }
    }

    /// <summary>
    /// Convert DataFields string to data field list
    /// </summary>
    internal List<String> DataFieldList
    {
        get
        {
            if (string.IsNullOrWhiteSpace(DataFields)) return new List<String>();

            string[] _array = DataFields.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            List<String> _fields = new List<String>(_array);

            for (int _i = _fields.Count - 1; _i >= 0; _i--)
            {
                _fields[_i] = _fields[_i].Trim();
                if (string.IsNullOrWhiteSpace(_fields[_i])) _fields.RemoveAt(_i);
            }

            return _fields;
        }

        set
        {
            StringBuilder _buffer = new StringBuilder();

            foreach (string _field in value)
            {
                if (_buffer.Length > 0) _buffer.Append(",");
                _buffer.Append(_field);
            }

            DataFields = _buffer.ToString();
        }
    }

    #endregion Properties
}

}

I'm an intern unit testing in C# so go easy on me.

I haven't had too many problems with my other projects but I can't seem to figure out how I'm suppose to unit test the internal List.

This is the code that I have so far for my unit test:

    [TestMethod]
    public void DataFields_Test()
    {
        ColumnDataField questionText = new ColumnDataField();
        questionText.DataFields = "test";
        string expected = "test";
        string actual = questionText.DataFields;
        Assert.AreEqual(expected, actual);
    }      

So that will run the DataFields property but other than that it's not going over any of the other code. I've been searching online for days trying to figure out the best way to go about this. I don't need to be told exactly what to do but guidance would be greatly appreciated.

1
  • I'd suggest to them that the code be refactored. There's way too much logic in that property. Commented Jun 12, 2012 at 19:07

1 Answer 1

4

Option #0: Consult your boss about how they expect internal methods to be tested.

Option #1: Add InternalsVisibleTo to your assembly under test and call DataFieldList directly.

[assembly:InternalsVisibleTo("YourTestAssemblyName")]

Option #2: Test something that calls the internal property that would let you access the results.

Option #3: Just don't test internal properties/fields/methods/classes/etc.

There are more options I'm sure...

Option #ProbablyShouldn't: You can use reflection to "find" the internal property but you probably shouldn't do that.


As an aside, you can greatly simplify the code in the untested property:

//.Net 4
get
{
    var cleansed = DataFields.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                             .Select(df => df.Trim())
                             .Where(str => !string.IsNullOrWhitespace(str));
    return new List<string>(cleansed);
}
set
{
    DataFields = string.Join(",", value);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Option #0 is actually a good idea. Maybe this has been decided in your project already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.