如何模拟SoapException使用Moq进行单元测试错误处理 [英] How to mock SoapException using Moq to unit test error handling

查看:306
本文介绍了如何模拟SoapException使用Moq进行单元测试错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个调用SOAP Web服务的小型控制台应用程序。嵌套的try-catches是以各种方式记录异常的一个悲剧性的混乱,我想包含一些关于SoapException被抛出的行为的测试报告。



问题:当我无法模拟一个界面,我不能使用Moq的类,如SoapException,我无法使用属性或方法虚拟?



更多的解释:



要测试此错误处理,我需要控制SoapException对象的 Actor 属性,以及 Detail 属性,以验证错误处理。



我的单元测试代码片段:

  [TestMethod] 
public void MyTestMethod()
{
Mock< SoapException> soapMock = new Mock< SoapException>(MockBehavior.Strict);

soapMock.SetupGet< string>(ex => ex.Actor).Returns(Test Actor);

由于我在嘲笑一个具体的类,而 Actor 属性未标记为虚拟,在测试运行期间执行 SetupGet(...)行时,Moq会抛出此异常:


System.NotSupportedException:非虚拟(VB中可覆盖)成员设置无效:ex => ex.Actor


经过一些阅读,理想的答案是模拟一个界面,我在这种情况下无法做到这一点。由于这是.NET框架中的一个类,我也不能将 Actor 属性虚幻地标记为



我如何模拟一个SoapException,或者有不同的方式可以验证错误处理?



除此之外,我首先要创建一个实际的SoapException里面有一些XML节点,但是我很快就以编程方式创建了一个XML文档树。我可以这样做,但是它需要更多的测试设置代码行,我想避免如果可能的话。

解决方案

不可能用Moq来模拟它。一个选择是使用 Microsoft Fakes Framework 中的 Shim 。您可以在您尝试嘲笑的代码不使用界面或虚拟方法(如您的方案)的情况下使用它。



请参见下面的说明Shim从msdn。


一个垫片在运行时修改了应用程序的编译代码,所以
而不是指定方法调用,它运行您的测试提供的垫片
。垫片可用于替换不能修改的
程序集的调用,如.NET程序集。


http://msdn.microsoft.com/en-us/library/hh549175.aspx



请注意,Fakes框架仅适用于Visual Studio Ultimate或Premium。


I've inherited a small console application that makes calls to a SOAP web service. It's a tragic mess of nested try-catches that log exceptions in various ways, and I'd like to wrap some test coverage around how it behaves when a SoapException gets thrown.

Question: How can I mock a class like SoapException using Moq when I can't mock an interface and I can't make properties or methods "virtual"?

A little more explanation:

To test this error handling, I need to control the Actor property of the SoapException object, as well as the Detail property in order to verify the error handling.

A snippet of my unit test code:

[TestMethod]
public void MyTestMethod()
{
    Mock<SoapException> soapMock = new Mock<SoapException>(MockBehavior.Strict);

    soapMock.SetupGet<string>(ex => ex.Actor).Returns("Test Actor");

Since I'm mocking a concrete class, and the Actor property is not marked "Virtual", Moq is throwing this exception when executing the SetupGet(...) line during the test run:

System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member: ex => ex.Actor

After some reading, the ideal answer is to mock an interface, which I am unable to do in this case. Since this is a class baked in to the .NET framework, I also can't magically mark the Actor property as virtual.

How can I mock a SoapException, or is there a different way I can verify the error handling?

As an aside, I first dove into creating an actual SoapException with some XML nodes inside it, but I quickly fell down the rabbit hole of programmatically creating an XML document tree. I can do it, but it would require many more lines of test setup code, which I'd like to avoid if possible.

解决方案

Its not possible to mock it using Moq. One option is to use Shim from Microsoft Fakes Framework. You can use it in scenarios where the code you are trying to mock doesn't use interface or is a virtual method(like in your scenario).

See below explanation of Shim from msdn.

A shim modifies the compiled code of your application at run time so that instead of making a specified method call, it runs the shim code that your test provides. Shims can be used to replace calls to assemblies that you cannot modify, such .NET assemblies.

http://msdn.microsoft.com/en-us/library/hh549175.aspx

Note that Fakes framework is only available with Visual Studio Ultimate or Premium.

这篇关于如何模拟SoapException使用Moq进行单元测试错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆