Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Use ConfigurationManager inside Unit Test Project

Tags:

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 aspx pages)

ProjectCore (all C# files - model)

ProjectTest (all tests)

in my ProjectCore, I am able to access the ConfigurationManager object from System.Configuration and pass information onto the project. However, when I ran a test where the ConfigurationManager is involved, I get the error

System.NullReferenceException: Object reference not set to an instance of an object. 

Here is an example of the test

using System.Configuration;  [TestMethod] public void TestDatabaseExists() {     //Error when I declare ConfigurationManager     Assert.IsNotNull(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString } 

in my other tests, ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString is what I set my data adapter's configuration string to, and returns a null error on the tests but not when I actually use the website. Any ideas?

like image 285
willykao Avatar asked Jul 10 '13 20:07

willykao


1 Answers

It could be one of several issues:

  1. You didn't add app.config to your ProjectTest project.
  2. You didn't add connection string in your app.config.

like image 179
Oleksii Aza Avatar answered Sep 21 '22 06:09

Oleksii Aza