How to mock SoapException using Moq to unit test error handling

女生的网名这么多〃 提交于 2019-12-04 11:18:58

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.

I was in a same situation to mock SoapException. I used the constructor of SoapException to pass Detail, Actor etc. Below is using Rhino Mocks.

//Build your xmlDocument 
    SoapException mockSoapException = MockRepository.GenerateMock<SoapException>("Mock SoapException", new XmlQualifiedName(), "TEST", xmlDocument); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!