Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
888 views
in Technique[技术] by (71.8m points)

visual studio - The problem with NUnit and app.config

When i run a simple test on connection to DB check i receive an error in NUnit:

[Test]
public void TestConn()
{
    string  connectionString = ConfigurationManager.ConnectionStrings["FertigungRead"].ConnectionString;
    SqlConnection connection = new SqlConnection(connectionString);
    connection.Open();
    Assert.AreEqual(ConnectionState.Open, connection.State);
    connection.Close();
 }

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

on line :

connectionString = ConfigurationManager.ConnectionStrings["FertigungRead"].ConnectionString;

Can i use ConfigurationManager in tests?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, you can. You need to be sure that any configuration you are referencing in your tests actually exist in the app.config of the test project.

In other words, the project where your test is in, does not have a connection string "FertigungRead" defined in its app.config.

One way to do this is to add the app.config of the system under test to the test project as a link, this way any changes happen on both projects.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...