asp: ConfigurationManager of Unit Testing

Source: Internet
Author: User
Tags connectionstrings

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.