c# - ConfigurationManager.GetSection(sectionName) returns null while performing unit tests -
i have unit tests project it's own app.config
file, mock of real configuration file defined target project being tested. mock file loaded , processed unit test code (not target project), , works if run tests within 1 test project.
configurationmanager.getsection(sectionname)
however, if run tests several test projects, , other test projects performed prior relevant project, above statement returns null
. if discussed test project performed first, there no problem loading configuration file.
how can fix loading of configuration file in unit test work correctly?
your problem not configurationmanager.getsection(sectionname) returns null, how can test code containing configurationmanager.getsection(sectionname)?
and answer is: wrap it, inject it, test mock it.
you have several examples of pepole facing same issue:
- http://chrisondotnet.com/2011/05/configurationmanager-wrapper-for-unit-testing/
- http://weblogs.asp.net/rashid/archive/2009/03/03/unit-testable-configuration-manager.aspx
(the second 1 more detailed, still idea same).
anyway, quite logical cannot use information app.config in unit test, app.config contextual whole application, when required write test absolutely independant. if use directly app.config value, have non logical coupling.
Comments
Post a Comment