如何测试仅在系统路径错误时抛出的异常?

发布于 2024-08-02 20:21:45 字数 515 浏览 3 评论 0原文

我在 C# 中有以下类,它从专有的 .DLL 创建一个对象,该对象在初始化时需要在可访问的目录中获得许可证。

public CplexServices{
    private Cplex _cplex;
    public Cplex Cplex { get { return _cplex; } }
    public CplexServices()
    {
        try
        {
            _cplex = new Cplex();
        }
        catch
        {
            throw new Exception("Path or license file is wrong");
        }
    }
}

如果 Windows 系统路径错误或许可证文件不正确,“new Cplex()”可能会失败。我想编写一个测试来断言当路径和/或许可证文件错误时是否抛出正确的异常。

如何在不更改路径或许可证文件的情况下编写此测试?

I have the following class in C# which creates an object from a propriatery .DLL that requires a license in a reachable directory at the initialization .

public CplexServices{
    private Cplex _cplex;
    public Cplex Cplex { get { return _cplex; } }
    public CplexServices()
    {
        try
        {
            _cplex = new Cplex();
        }
        catch
        {
            throw new Exception("Path or license file is wrong");
        }
    }
}

"new Cplex()" might fail if the Windows system path is wrong or the license file is not correct. I want to write a test to assert if the correct exception is thrown when the path and/or license file is wrong.

How can I write this test without changing the path or the license file?

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

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

发布评论

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

评论(3

失与倦" 2024-08-09 20:21:45

你在测试什么?我相信您正在测试调用 new Cplex() 的错误处理路径是否正确。

public CplexServices()
{
    try
    {
        _cplex = new Cplex();
    }
    catch
    {
        throw new Exception("Path or license file is wrong");
    }
}

您不是在测试 new Cplex() 本身,它在许可证文件丢失时会抛出异常,而是在测试如果它抛出异常,您会做正确的事情。在这种情况下,它是如此微不足道的正确,我可能不会太在意。然而,如果这是相当复杂的,例如需要进行一些严重的恢复处理,那么我们更希望能够对此进行测试。

一进场,就经过一座工厂。

public CplexServices(CplexFactory myFactory)
{
    try
    {
        _cplex = myFactory.makeCplex();
    }
    catch
    {
        throw new Exception("Path or license file is wrong");
    }
}

现在,在您的测试中,您可以传递一个工厂,该工厂可以人为地抛出触发您要测试的路径的异常。

这仍然留下了如何测试 CPLEX() 构造函数本身的问题。可以说这不是你的问题,作者应该有自己的单元测试。

What are you testing? I believe that you are testing that the error handling path for the call to new Cplex() is correct.

public CplexServices()
{
    try
    {
        _cplex = new Cplex();
    }
    catch
    {
        throw new Exception("Path or license file is wrong");
    }
}

You are not testng new Cplex() itself, that it throws exceptions when the licence file is missing rather, you are testing that if it throws exceptions you do the right thing. In this case it's so trivially correct that I probably would not care too much. However if this were rather more complex, some serious recovery processing to do for example, then we'd much prefer to be able to test this.

An approach, pass in a factory.

public CplexServices(CplexFactory myFactory)
{
    try
    {
        _cplex = myFactory.makeCplex();
    }
    catch
    {
        throw new Exception("Path or license file is wrong");
    }
}

Now in your tests you can pass a factory which can articficially throw the exception triggering the path you want to test.

This still leaves the question of how do you might test the Cplex() constructor itself. Arguably this is not your problem, the authors should have their own unit tests.

冰葑 2024-08-09 20:21:45

你不能。您需要为该文件设置一个错误位置,并确认您收到了异常。

但无论如何,这可能不是您需要进行单元测试的内容,因为确定失败的代码不取决于您。

我想您在配置中设置了该文件的位置?然后你需要测试该位置是否正确;但请考虑,如果您有多个部署和不同的配置,则需要针对正确的环境运行测试。

You can't. You'll need to set a false location for the file, and confirm that you get the exception.

But probably this isn't something you'd unit test anyway, as the code determining the fail is not up to you.

I presume you set the location for this file in a config? Then you'll need to just test that that location is correct; but consider that if you have multiple deployments, and different configs, you'll need to run your test against the right environment.

蓝戈者 2024-08-09 20:21:45

您可以使用

string oldPath = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", "MyBadPath");
// Do tests
Environment.SetEnvironmentVariable("PATH", oldPath);

“要测试路径问题”,放入错误的路径。要测试许可证文件,请输入包含错误许可证文件的测试目录的路径。

You could use

string oldPath = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", "MyBadPath");
// Do tests
Environment.SetEnvironmentVariable("PATH", oldPath);

To test a path problem, put in a bad path. To test the license file, put in a path to a test directory which holds a bad license file.

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