本文介绍了如何在没有公共构造函数的情况下模拟/伪造/存根密封OracleException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试中,我需要测试引发OracleException时发生的情况(由于存储过程失败).我正在尝试将Rhino Mocks设置为

In my tests I need to test what happens when an OracleException is thrown (due to a stored procedure failure). I am trying to setup Rhino Mocks to

Expect.Call(....).Throw(new OracleException());

但是,无论出于何种原因,OracleException似乎都没有公共构造函数而被密封.我该怎么做才能对此进行测试?

For whatever reason however, OracleException seems to be sealed with no public constructor. What can I do to test this?

这正是我要实例化的内容:

Here is exactly what I'm trying to instantiate:

public sealed class OracleException : DbException {
  private OracleException(string message, int code) { ...}
}

推荐答案

Oracle似乎在更高版本中更改了其构造函数,因此上述解决方案将不起作用.

It seems that Oracle changed their constructors in later versions, therefore the solution above will not work.

如果您只想设置错误代码,则可以使用以下方法解决2.111.7.20:

If you only want to set the error code, the following will do the trick for 2.111.7.20:

ConstructorInfo ci = typeof(OracleException)
            .GetConstructor(
                BindingFlags.NonPublic | BindingFlags.Instance, 
                null, 
                new Type[] { typeof(int) }, 
                null
                );

Exception ex = (OracleException)ci.Invoke(new object[] { 3113 });

这篇关于如何在没有公共构造函数的情况下模拟/伪造/存根密封OracleException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 23:53