在单元测试项目中无法使用ConfigurationManager [英] Cannot Use ConfigurationManager inside Unit Test Project
问题描述
我想为我的项目写一个单元测试,但它不会让我使用配置管理器。现在我的项目设置为
ASP.Net应用程序(所有aspx页面)
ProjectCore C#files - model)
ProjectTest(所有测试)
在我的ProjectCore中,我可以访问ConfigurationManager对象从System.Configuration并传递信息到项目。但是,当我运行一个测试,其中涉及ConfigurationManager,我得到错误
System.NullReferenceException:对象引用未设置为对象的实例。
以下是测试示例
使用System.Configuration;
[TestMethod]
public void TestDatabaseExists()
{
//当我声明ConfigurationManager时出错
Assert.IsNotNull(ConfigurationManager.ConnectionStrings [ConnectionString ] .ConnectionString
}
在我的其他测试中,ConfigurationManager.ConnectionStrings [ConnectionString ] .ConnectionString是什么我设置我的数据适配器的配置字符串,并返回一个null错误的测试,但不是当我实际使用网站。
这可能是几个问题之一:
- 您没有添加app.config到您的ProjectTest
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?
It could be one of several issues:
- You didn't add app.config to your ProjectTest project.
You didn't add connection string in your app.config.
这篇关于在单元测试项目中无法使用ConfigurationManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!