When using a. NET profile with ConfigurationManager, you can add a configuration file for unit testing, although it can be tested but not decoupled. Using Iconfigurationmanager and Configurationmanagerwrapper to fit the ConfigurationManager is a better way to Configurationmanagerwrapper provides implementations of the. NET configuration file, and if you need to support additional configurations, create a different implementation class for the Iconfigurationmanager interface.
1. Defining the Iconfigurationmanager interface
The code that originally relied on configurationmanager now relies on Iconfigurationmanager. A mock that can be handy during unit testing.
Public Interface iconfigurationmanager{ get;} Get ; } Object GetSection (string sectionname);}
2. Create an adaptation class Configurationmanagerwrapper
Non-unit test environments use Configurationmanagerwrapper as the default implementation of Iconfigurationmanager.
Public classconfigurationmanagerwrapper:iconfigurationmanager{ PublicNameValueCollection AppSettings {Get { returnconfigurationmanager.appsettings; } } Publicconnectionstringsettingscollection ConnectionStrings {Get { returnconfigurationmanager.connectionstrings; } } Public ObjectGetSection (stringsectionname) { returnconfigurationmanager.getsection (sectionname); }}
3. Custom generic Configuration Interface
When our code needs to use configuration, you can consider creating a generic generic interface or using a dedicated, strongly typed interface. This demonstrates the use of a common interface.
Public Interface iconfiguration{ t Get<T> (string key, T @default);}
4. Implement the generic interface configuration interface. NET configuration file version
Appconfigadapter does not use ConfigurationManager directly but relies on the Iconfigurationmanager interface.
Public classappconfigadapter:iconfiguration{PrivateIconfigurationmanager _configurationmanager; PublicAppconfigadapter (Iconfigurationmanager configurationmanager) { This. _configurationmanager =ConfigurationManager; } PublicT get<t> (stringNodeName, T @default) { varValue = This. _configurationmanager.appsettings[nodename]; returnValue = =NULL? @default: (T) Convert.changetype (value,typeof(T)); }}
5. Unit test the implementation of the generic configuration interface
Use the most popular unit test framework and Mock Class Library: XUNIT+MOQ for unit testing.
Public classappconfigadaptertest{[Fact] Public voidgetstringtest () {varKey ="Key"; varValue ="value"; varConfiguration =NewAppconfigadapter ( This. Getconfigurationmanager (o =O.add (key, value. ToString ())); Assert.equal (configuration. Get (Key,string. Empty), value); } [Fact] Public voidgetinttest () {varKey ="Key"; varValue =1; varConfiguration =NewAppconfigadapter ( This. Getconfigurationmanager (o =O.add (key, value. ToString ())); Assert.equal (configuration. Get (Key,int. MinValue), value); } [Fact] Public voidgetbooltest () {varKey ="Key"; varValue =true; varConfiguration =NewAppconfigadapter ( This. Getconfigurationmanager (o =O.add (key, value. ToString ())); Assert.equal (configuration. Get (Key,false), value); } [Fact] Public voidgetdatetimetest () {varKey ="Key"; varValue =DateTime.Parse (DateTime.Now.ToString ()); varConfiguration =NewAppconfigadapter ( This. Getconfigurationmanager (o =O.add (key, value. ToString ())); Assert.equal (configuration. Get (Key, Datetime.minvalue), value); } [Fact] Public voidgetdecimaltest () {varKey ="Key"; varValue =1.1m; varConfiguration =NewAppconfigadapter ( This. Getconfigurationmanager (o =O.add (key, value. ToString ())); Assert.equal (configuration. Get (Key,decimal. MinValue), value); } PrivateIconfigurationmanager Getconfigurationmanager (action<namevaluecollection>Set) { varAppSettings =NewNameValueCollection (); Set(appSettings); varConfigurationManager =NewMock<iconfigurationmanager>(); Configurationmanager.setup (o=o.appsettings). Returns (appSettings); returnConfigurationmanager.object; }}
6. Summary
Converts code that relies on ConfigurationManager static classes to a dependent Iconfigurationmanager interface, and the runtime injects Configurationmanagerwrapper implementation classes. A mock Iconfigurationmanager object is used when unit tests are simulated.
asp: ConfigurationManager of Unit Testing