简体   繁体   English

单元测试意外异常?

[英]unit testing an unexcpected exception?

Is it possible to mock or unit test for an unexpected exception? 是否可以对意外的异常进行模拟或单元测试? Code coverage is complaining that the catch is not being tested for, but how do you test it? 代码覆盖范围抱怨没有对该捕获进行测试,但是您如何对其进行测试?

    public List<Blog> SelectActiveBlogs()
    {
        List<Blog> returnCode = null;

        try
        {
            returnCode = GetQueryable<Blog>().Where(b => b.IsActive).ToList();
        }
        catch (Exception ex)
        {
            ExceptionHandler.HandleException(ex);
        }

        return returnCode;
    }

EDIT: The GetQueryable is a method that uses a repository factory to get a db result, so there could be exceptions bubbling up from db interactions in production, so I want to make sure we catch them if something goes wrong an thus the catch block is present. 编辑: GetQueryable是一种使用存储库工厂获取数据库结果的方法,因此在生产环境中可能会因数据库交互而冒泡,因此我想确保如果出现问题,我们可以捕获它们,因此catch块是当下。

EDIT2: The exception is actually handled by a custom class to preserve the stack trace, once logged it's not re thrown. EDIT2:异常实际上是由自定义类处理的,以保留堆栈跟踪,一旦记录,则不会重新引发该异常。 Question still remains as to whether I can mock and throw an exception in this case. 在这种情况下,我是否可以嘲笑并引发异常仍然存在疑问。

I'd presume you don't test for it. 我以为你不做测试。

I've left behind many exception handlers for cases that can't be reached in test systems before. 对于以前在测试系统中无法实现的案例,我已经留下了许多异常处理程序。

In this particular case, you should just remove the try / catch / throw because that's nonsense code anyway. 在这种情况下,您应该只删除try / catch / throw,因为无论如何这都是废话。

You can handle that using the ExpectedException Attribute, even if its unexcepeted. 您可以使用ExpectedException属性处理该事件,即使该属性没有超出范围。 Its depend on the typeof parameter that you pass to the argument. 它取决于您传递给参数的typeof参数。

[TestMethod]
[ExpectedException(typeof(Exception),
"A exception has been throws.")]
public List<Blog> SelectActiveBlogs()
{
    List<Blog> returnCode = null;

    try
    {
        returnCode = GetQueryable<Blog>().Where(b => b.IsActive).ToList();
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return returnCode;
}

Another thing, if you implement it this way you dont need a try catch block. 另一件事,如果您以这种方式实现它,则不需要try catch块。 If you catch an exception and only rethrow it (without any other exception handling) it makes no sense. 如果捕获到异常并仅将其重新抛出(不进行任何其他异常处理),则没有任何意义。

Hope that helps, if not please leave a comment. 希望能有所帮助,如果不能,请发表评论。 Have a nice day! 祝你今天愉快!

Don't mock the method GetQueryable() but the class that is is responsible to get the queryable. 不要模拟方法GetQueryable(),而是负责获取可查询对象的类。

I guess you have some code like this in GetQueryable() 我猜你在GetQueryable()中有一些这样的代码

private IQueryable<T> GetQueryable<T>()
{
     return repository.query...
}

So don't try to mock the method but the repository instance and throw an exception in the mock when query is called. 因此,请勿尝试模拟方法,而应模拟存储库实例,并在调用查询时在模拟中引发异常。

Mocking that using MoQ should not be to complicated - something like 嘲笑使用最小起订量不应该太复杂-类似

var mock = new Moq.Mock<IRepository<Blog>>();
mock.Setup(r => r.Query()).Throws(new Exception("I'm really unexpected"));

I'm in doubt if this test will lead to a greater code quality or less bugs. 我对此测试是否会导致更高的代码质量或更少的错误感到怀疑。 But the above method will please the coverage tool ;) 但是以上方法将请覆盖工具;)

If you want to get into the exception , you can mock the call GetQueryable<Blog>() and return NULL , that should make the code throw exception . 如果要进入exception ,可以模拟调用GetQueryable<Blog>()并返回NULL ,这将使代码抛出exception Or you can just throw an exception in mocking that call, depends on how you implement the mock. 或者,您可以在模拟该调用时引发异常,这取决于您如何实现模拟。 BTW, you will lose the stack trace in that code if you do throw ex . 顺便说一句,如果您确实throw ex ,则将丢失该代码中的堆栈跟踪。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM