Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing a static class with a private static field that is a common dependency
    primarykey
    data
    text
    <p>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:</p> <pre><code>The type initializer for 'Foo.Bar.Controllers.DataService' threw an exception. </code></pre> <p>the inner exception says:</p> <pre><code> {"Value cannot be null.\r\nParameter name: value"} at System.Boolean.Parse(String value) </code></pre> <p>The class I'm trying to test is:</p> <p>namespace Foo.Bar.Controllers {</p> <pre><code> public static class DataService { private static ClassINeedOneInstanceOf _dataGettingClass = new ClassINeedOneInstanceOf(); public static List&lt;Info&gt; GetServiceInfoList(String svcName) { List&lt;Info&gt; 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 } } </code></pre> <p>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</p> <p>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?</p> <p>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?</p> <p>Thanks!</p> <p>Edit: The unit test class that calls the method is:</p> <pre><code>namespace Foo.Test { [TestClass] public class DataServiceTests { [TestMethod] public void GetInfoListUsingServiceName() { string serviceName = "service001"; var result = DataService.GetServiceInfoList(serviceName); Assert.IsNotNull(result); } } } </code></pre> <p>and the line that is reference by the parse inner exception is:</p> <pre><code>private static ClassINeedOneInstanceOf _dataGettingClass = new ClassINeedOneInstanceOf(); </code></pre> <p>in the DataService class</p> <p>The error {"Value cannot be null.\r\nParameter name: value"} came from:</p> <pre><code>bool testDataOnly = Boolean.Parse(ConfigurationSettings["TestDataOnly"]); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload