In my C# class project, I have a helper class which has the following property
public class Helper
{
public string ConnectionString
{
get
{
return ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
}
}
}
My following test fails when I call the Helper class from NUNIT project with error message: Failure: System.NullReferenceException : Object reference not set to an instance of an object.
[Test]
public void connection_string_exists()
{
string connection = new Helper().ConnectionString;
Assert.NotNull(connection);
}
If I run the line of code new Helper().ConnectionString from a asp.net project then it works. Why does the Test fail?
Please let me know.
I suspect your Nunit tests are part of a different project and when you run the tests, ConfigurationManager looks at the config file of your test project and does not find “MyConnectionString”