Reputation: 2210
I have a public static class I'm trying to unit test using Visual Studio 2012's built in testing framework. When I try to run unit tests on it I get this error:
The type initializer for 'Foo.Bar.Controllers.DataService' threw an exception.
the inner exception says:
{"Value cannot be null.\r\nParameter name: value"}
at System.Boolean.Parse(String value)
The class I'm trying to test is:
namespace Foo.Bar.Controllers {
public static class DataService {
private static ClassINeedOneInstanceOf _dataGettingClass = new ClassINeedOneInstanceOf();
public static List<Info> GetServiceInfoList(String svcName) {
List<Info> infoList = null;
if (ERROR CHECKING AND STUFF) {
infoList = _dataGettingClass.GetInfoFromAwesomeOtherService(svcName);
}
return infoList
}
// other methods like the one above that return data for other things and in different ways but they
// are all public static, return data, and use a method from _dependecyGettingClass
}
}
My understanding of static classes with static fields is that the first time the class is called the field instantiates and can be used from then on. This is backed up by my code actually working, ex: the website uses it to get data and such
Is the unit test framework doing something weird and not calling the class in the same way as "typical" c# code does? If so, is there a way I can change the mstest code?
Also, with this use pattern is my code architecture and design correct? Should this class (with its reliance on one instance of _dataGettingClass) be written differently?
Thanks!
Edit: The unit test class that calls the method is:
namespace Foo.Test
{
[TestClass]
public class DataServiceTests
{
[TestMethod]
public void GetInfoListUsingServiceName()
{
string serviceName = "service001";
var result = DataService.GetServiceInfoList(serviceName);
Assert.IsNotNull(result);
}
}
}
and the line that is reference by the parse inner exception is:
private static ClassINeedOneInstanceOf _dataGettingClass = new ClassINeedOneInstanceOf();
in the DataService class
The error {"Value cannot be null.\r\nParameter name: value"} came from:
bool testDataOnly = Boolean.Parse(ConfigurationSettings["TestDataOnly"]);
Upvotes: 3
Views: 1990
Reputation: 5132
the configuration file (web.config or app.config) that is used by the test framework is missing the settings for "TestDataOnly"
.
Upvotes: 1