简体   繁体   English

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

[英]How do I test an exception that is only thrown when the system path is wrong?

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 . 我在C#中有以下类,该类从专有.DLL创建一个对象,该.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");
        }
    }
}

"new Cplex()" might fail if the Windows system path is wrong or the license file is not correct. 如果Windows系统路径错误或许可证文件不正确,则“ new Cplex()”可能会失败。 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? 如何编写此测试而不更改路径或许可证文件?

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. 但是请考虑一下,如果您有多个部署以及不同的配置,则需要在正确的环境下运行测试。

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. 要测试许可证文件,请放入包含错误许可证文件的测试目录的路径。

What are you testing? 你在测试什么? I believe that you are testing that the error handling path for the call to new Cplex() is correct. 我相信您正在测试对新Cplex()的调用的错误处理路径是否正确。

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. 您不是在测试新的Cplex()本身,而是在缺少许可证文件时抛出异常,而是测试是否在抛出异常时执行正确的操作。 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. 剩下的问题仍然是如何测试Cplex()构造函数本身。 Arguably this is not your problem, the authors should have their own unit tests. 可以说这不是您的问题,作者应该有自己的单元测试。

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

相关问题 我如何找到为什么我得到“抛出'System.Web.HttpUnhandledException'类型的异常”? - How do I find why I get "Exception of type 'System.Web.HttpUnhandledException' was thrown"? 如何处理异步方法引发的混乱异常? - How do I handle the messy exception that is thrown by an async method? 当被测系统利用外部静态依赖项时,如何编写单元测试? - How do I write a unit test when the system under test utilizes external static dependencies? 为什么运行Response.Redirect()时会出现抛出异常? - Why do I get a thrown exception when I run Response.Redirect()? 实例化类SmsNamedValuesDictionary的对象时引发System.typeload异常 - System.typeload exception thrown when instantiating object of class SmsNamedValuesDictionary 我如何知道是否故意抛出了异常? - How can i tell if an Exception was deliberately thrown? 如何测试属性是否在NUnit测试中引发异常? - How do I test that a property throws an exception in an NUnit test? 仅在抛出异常时将堆栈跟踪写入日志文件 - Writing stack trace to log file only when exception thrown 如何检查带有xUnit(.net核心)的Integration Test中是否引发了异常? - How to check if exception is thrown in Integration Test with xUnit (.net core)? 单元测试如何确认已抛出异常 - How can a unit test confirm an exception has been thrown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM