c# - Unit testing a void method -
c# - Unit testing a void method -
i understand can unit test void method checking effects. however, looking @ loadconfigfile method in code:
internal xmldocument configdata; public configfile() { configdata = new xmldocument(); } /// <summary> /// load config file memory /// </summary> /// <param name="filename">path , filename of config file</param> public void loadconfigfile(string filename) { if(string.isnullorempty(filename)) throw new system.argumentexception("you must specify filename"); seek { configdata.load(filename); } catch(exception ex) { throw new exception("config file not loaded",ex); } }
it loads config file private field - needs kept private developers not modify value directly. instead modification done through setconfigvalue , getconfigvalue methods (which assume need tested separately).
given this, how test loadconfigfile worked? cant access private configdata field.
where else in class configdata
used?
if, say, have method like
public string getvalue() { homecoming configdata.getsomedatafromthis; }
then suggest have test so:
public void readvaluefromloadedconfigdata() { // arrange. const string expectedvalue = "whatever"; var sut = new configfile(); sut.loadconfigfile(@"c:\pathtotheconfigfile"); // act. string actual = sut.getconfigvalue(); // assert. assert.areequal(expectedvalue, actual); }
in tests, seek test public interactions, since going deep class, having read values of private fields, means class isn't test friendly.
c# unit-testing
Comments
Post a Comment