如何在没有公共构造函数的情况下模拟/伪造/存根密封 OracleException?
在我的测试中,我需要测试抛出 OracleException(由于存储过程失败)时会发生什么。 我正在尝试将 Rhino Mocks 设置为
Expect.Call(....).Throw(new OracleException());
无论出于何种原因,OracleException 似乎都没有公共构造函数来密封。 我可以做什么来测试这个?
编辑:这正是我想要实例化的内容:
public sealed class OracleException : DbException {
private OracleException(string message, int code) { ...}
}
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());
For whatever reason however, OracleException seems to be sealed with no public constructor. What can I do to test this?
Edit: Here is exactly what I'm trying to instantiate:
public sealed class OracleException : DbException {
private OracleException(string message, int code) { ...}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
对于 Oracle 的托管数据访问(v 4.121.1.0),构造函数再次更改
For oracle's managed data access (v 4.121.1.0) the constructor changed again
看来Oracle在后来的版本中改变了他们的构造函数,因此上面的解决方案不起作用。
如果您只想设置错误代码,则以下内容适用于 2.111.7.20:
It seems that Oracle changed their constructors in later versions, therefore the solution above will not work.
If you only want to set the error code, the following will do the trick for 2.111.7.20:
操作方法如下:
感谢所有提供帮助的人,您已被投票
Here is how you do it:
Thanks to all that helped, you have been upvoted
我正在使用 Oracle.DataAccess.Client 数据提供程序客户端。 我在构造 OracleException 对象的新实例时遇到问题,但它一直告诉我没有公共构造函数。 我尝试了上面显示的所有想法,但不断收到空引用异常。
调试测试代码时,我总是得到“ci”的 NULL 值。
Oracle 是否更改了库以不允许这样做? 我做错了什么以及需要做什么来实例化 OracleException 对象以与 NMock 一起使用?
顺便说一下,我使用的是 10g 版本的客户端库。
谢谢,
查理
I'm using the Oracle.DataAccess.Client data provider client. I am having trouble constructing a new instance of an OracleException object, but it keeps telling me that there are no public constructors. I tried all of the ideas shown above and keep getting a null reference exception.
When debugging the test code, I always get a NULL value for 'ci'.
Has Oracle changed the library to not allow this? What am I doing wrong and what do I need to do to instantiate an OracleException object to use with NMock?
By the way, I'm using the Client library for version 10g.
Thanks,
Charlie
使用反射实例化OracleException对象? 代替
用。。。来
Use reflection to instantiate the OracleException object? Replace
with
使用反射来实例化 OracleException。 请参阅 这篇博文
Use reflection to instantiate OracleException. See this blog post
您始终可以获得像这样的所有构造函数
For
Oracle.DataAccess
4.112.3.0 这返回了 7 个构造函数我想要的是列表中的第二个,它有 5 个参数,
int,字符串,字符串,字符串,int
。 我对第五个参数感到惊讶,因为在 ILSpy 中它看起来像这样所以,为了获得我想要的构造函数,我最终使用了
You can always get all the constructors like this
For
Oracle.DataAccess
4.112.3.0 this returned 7 constructorsThe one I wanted was the second one in the list which took 5 arguments,
int, string, string, string, int
. I was surprised by the fifth argument because in ILSpy it looked like thisSo, to get the constructor I wanted I ended up using
很好的解决方案乔治。 这也适用于 SqlException:
-dave
Good solution George. This also works for SqlException too:
-dave
您可以编写一个每次都会失败/错误的简单存储过程,然后用它来测试吗?
Can you write a trivial stored procedure that fails/errors each time, then use that to test?