c# - Unit Testing Internal List<String> -
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 { { return _columnindex; } set { _columnindex = value; } } /// <summary> /// data fields /// </summary> public string datafields { { return _datafields; } set { _datafields = value; } } /// <summary> /// convert datafields string data field list /// </summary> internal list<string> datafieldlist { { if (string.isnullorwhitespace(datafields)) return new list<string>(); string[] _array = datafields.split(new char[] { ',' }, stringsplitoptions.removeemptyentries); list<string> _fields = new list<string>(_array); (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 intern unit testing in c# go easy on me.
i haven't had many problems other projects can't seem figure out how i'm suppose unit test internal list.
this code have far 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 run datafields property other it's not going on of other code. i've been searching online days trying figure out best way go this. don't need told guidance appreciated.
option #0: consult boss how expect internal methods tested.
option #1: add internalsvisibleto assembly under test , call datafieldlist
directly.
[assembly:internalsvisibleto("yourtestassemblyname")]
option #2: test calls internal property let access results.
option #3: don't test internal properties/fields/methods/classes/etc.
there more options i'm sure...
option #probablyshouldn't: can use reflection "find" internal property shouldn't that.
as aside, can simplify code in untested property:
//.net 4 { 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); }
Comments
Post a Comment