简体   繁体   English

使用作为公共依赖项的私有静态字段对静态类进行单元测试

[英]Unit testing a static class with a private static field that is a common dependency

I have a public static class I'm trying to unit test using Visual Studio 2012's built in testing framework. 我有一个公共静态类,我正在尝试使用Visual Studio 2012的内置测试框架进行单元测试。 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 { 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? 单元测试框架是否做了一些奇怪的事情并没有像“典型”c#代码那样调用类? If so, is there a way I can change the mstest code? 如果是这样,有没有办法可以改变mstest代码?

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? 这个类(依赖于_dataGettingClass的一个实例)是否应该以不同的方式编写?

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 在DataService类中

The error {"Value cannot be null.\\r\\nParameter name: value"} came from: 错误{“值不能为空。\\ r \\ nParameter name:value”}来自:

bool testDataOnly = Boolean.Parse(ConfigurationSettings["TestDataOnly"]);

测试框架使用的配置文件(web.config或app.config)缺少"TestDataOnly"的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试静态实用程序类 - Unit testing static utility class 使用静态类/方法依赖项测试类 - Testing a Class with a Static Class/method dependency 内部静态类包含私有类。 如何使用Microsoft Fake创建私有类的对象以进行单元测试 - Internal static class contains private class. How to create object of private class for unit testing using microsoft fakes 非静态类中的私有静态方法的单元测试 - unit test for private static method in non-static class 对内部静态类中的void方法进行单元测试 - Unit testing a void method in an internal static class 单元测试工厂/服务定位器-静态类 - Unit Testing Factory/Service Locator - Static class 依赖注入和单元测试 - 静态辅助方法或私有实例方法 - dependecy injection and unit testing - static helper methods or private instance methods 在私有静态方法的C#中进行单元测试,接受其他私有静态方法作为委托参数 - Unit testing in C# of private-static method accepting other private-static method as a delegate parameter 私有静态依赖与私有依赖 - Private Static Dependency vs Private Dependency 通过调用包装HttpContext的静态类的单元测试类 - Unit Testing a class with a call to a Static class wrapping an HttpContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM