Cannot Use ConfigurationManager inside Unit Test Project

后端 未结 5 1766
孤独总比滥情好
孤独总比滥情好 2021-01-07 19:20

I\'m trying to write a unit test for my project, but it will not let me use the Configuration Manager. Right now my project is set up like

ASP.Net application (all a

5条回答
  •  北海茫月
    2021-01-07 20:07

    first of all you must make sure that you have an app.config file in your nunit tests project.

    To add it, you can open the project properties (right click on the project)

    Enter the details of your connection, it will generate a app.config file or add the right section within :

    In your Test class, add the reference to : System.Configuration; => using System.Configuration;

    For example you could use your connectionString by this way :

    [TestFixture]
    public class CommandesDALUnitTest
    {
    
        private string _connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    
        [Test]
        public void Method_Test()
        {
            string test = _connectionString;
                ....
        }
    }
    

提交回复
热议问题