调整静态类中的常量以进行 C# 中的单元测试

发布于 2024-07-29 04:07:43 字数 668 浏览 4 评论 0原文

我有一个代表连接池的静态类,我想使用 Visual Studio 2008 的内置单元测试框架对其进行单元测试。 静态类中有一些常量,例如允许的最大连接数。 我想为我的单元测试减少这个值,这样我就不必打开很多连接来达到测试条件(特别是达到允许的最大连接数时的代码)。 有没有办法可以在单元测试中编辑这个常量? 这对我来说似乎不可能。

我的一个解决方案是编写另一种方法来访问要测试的代码,该方法将“允许的最大连接数”作为参数。 这看起来有点脏,因为它修改了我想要测试的代码,即使只是轻微修改; 然而,还有更好的选择吗? 这是我的一些代码,供参考:

internal static class ConnectionPool<T> where T : Connection, new()
{
    private const int MAX_OBJECTS = 25;
    private static int _totalConnections;

    internal static T getConnection(bool testMode)
    {
        if (_totalConnections >= MAX_OBJECTS)
        {
            // Here's the code I want to test
        }
    }
}

I have a static class representing a connection pool that I want to unit test using Visual Studio 2008's built-in unit testing framework. The static class has some constants in it, like for the maximum allowed connections. I want to decrease this value for my unit test so I don't have to have a lot of connections open in order to hit a test condition (specifically, the code for when the maximum allowed connections has been reached). Is there a way I can edit this constant just in the unit test? That doesn't seem possible to me.

One solution I have is to write another method for accessing the code to be tested, one that would take 'maximum allowed connections' as a parameter. That seems slightly dirty in that it modifies the code I want to test, even if only slightly; however, is there a better alternative? Here's some of my code, for reference:

internal static class ConnectionPool<T> where T : Connection, new()
{
    private const int MAX_OBJECTS = 25;
    private static int _totalConnections;

    internal static T getConnection(bool testMode)
    {
        if (_totalConnections >= MAX_OBJECTS)
        {
            // Here's the code I want to test
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月静好 2024-08-05 04:07:43

不,您不能修改常量,但您可以将其替换为静态只读字段并使用反射修改该字段。

No, you can't modify constant, but you can replace it with static readonly field and modify that field using reflection.

冬天旳寂寞 2024-08-05 04:07:43

在执行此操作之前,您应该问自己此限制的重要性,以及更改它是否会改变测试的性质? 换句话说,如果我将 MAX_OBJECTS 更改为 5,我的代码是否或多或少容易受到资源争用或多线程问题导致的问题? 您是否因为太慢或太不切实际而避免测试“真实”条件? 如果是这样,也许最好将其视为不同类别的单元测试。 在我工作的地方,我们有许多至关重要但耗时的测试。 如果始终保留,它们将使持续集成变得不切实际,因此我们创建了 NUnit 类别“Nightly”,并且需要超过一分钟的测试必须带有 Nightly 的类别属性。

Before you do this, you should ask yourself about the importance of this limit and if by changing it are you changing nature of the tests? In other words, if I change MAX_OBJECTS to 5, is my code more or less vulnerable to problems that result from resource contention or multithreading issues? Is your avoidance of testing the "real" conditions because it is too slow or too impractical? If so, maybe it's best to treat this as a different class of unit test. Where I work, we have a number of tests that are vital but costly in terms of time. If left in at all times, they would make continuous integration impractical, therefore we created the NUnit category "Nightly" and tests that take longer than a minute must bear the Category attribute with Nightly.

一生独一 2024-08-05 04:07:43

您可以从配置文件中获取它,因此您可以使用不同的值进行测试。

You can get it from a configuration file, so you can have different values for testing.

闻呓 2024-08-05 04:07:43

我发现被测试代码的条件有两个部分:常量 MAX_OBJECTS 是其中之一,true,但还有 _totalConnections。 使用 Visual Studio 为我生成的访问器类来访问 ConnectionPool 中的私有方法和变量,我可以修改 _totalConnections 的值以使条件 <代码>if (_totalConnections >= MAX_OBJECTS) true。 换句话说,我将对单元测试中的静态类撒谎,这样我就不必创建 MAX_OBJECTS 连接来满足条件。

It occurred to me that there are two parts to the condition for the code being tested: the constant MAX_OBJECTS is one of them, true, but there is also _totalConnections. Using an accessor class, which Visual Studio generates for me to have access to private methods and variables within ConnectionPool, I can modify the value of _totalConnections in order to make the condition if (_totalConnections >= MAX_OBJECTS) true. In other words, I'll lie to the static class in my unit test so that I don't have to create MAX_OBJECTS connections in order to meet the condition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文