How to mock SoapException using Moq to unit test error handling

Greg Burghardt

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.

Adarsh Shah

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Using Moq to mock an asynchronous method for a unit test

From Dev

Mock HttpContext using moq for unit test

From Dev

Unit Test - How to MOQ WCF Client using MOQ

From Dev

Arranging Unit Test using Moq

From Dev

Arranging Unit Test using Moq

From Dev

How to unit test a class using moq that has dependencies within a method?

From Dev

How do i Unit Test my RelayCommand using Moq?

From Dev

How to unit test a class using moq that has dependencies within a method?

From Dev

How to Unit Test an MVC Controller with Three Dependencies, Using Moq

From Dev

How to mock springSecurityService in an unit test

From Dev

How to mock this unit test in Python?

From Dev

writing a test using moq unit testing framework

From Dev

Using Moq to unit test but the object returns Null

From Dev

IdependencyResolver is always null in unit test using moq

From Dev

How to check if a function was called in a unit test using pytest-mock?

From Dev

How to add an item to a Mock DbSet (using Moq)

From Dev

Redis unit test using redis mock

From Dev

Mocha Unit Test Error Handling from Library

From Dev

How to write unit test for private method in c# using moq framework?

From Dev

Mock same method in a test using Moq with different linq expressions

From Dev

How to android unit test and mock a static method

From Dev

how to mock $window to unit test AngularJS service?

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

How to mock the UserAgent property for an HttpRequest in a unit test?

From Dev

How to unit test this function without mock asserts?

From Dev

How to mock browserHistory in unit test environment?

From Dev

How to mock multiple components in camel unit test?

From Dev

How to mock an Akka Actor to Unit Test a class?

From Dev

How to mock a generic parameter for a unit test in Java?

Related Related

  1. 1

    Using Moq to mock an asynchronous method for a unit test

  2. 2

    Mock HttpContext using moq for unit test

  3. 3

    Unit Test - How to MOQ WCF Client using MOQ

  4. 4

    Arranging Unit Test using Moq

  5. 5

    Arranging Unit Test using Moq

  6. 6

    How to unit test a class using moq that has dependencies within a method?

  7. 7

    How do i Unit Test my RelayCommand using Moq?

  8. 8

    How to unit test a class using moq that has dependencies within a method?

  9. 9

    How to Unit Test an MVC Controller with Three Dependencies, Using Moq

  10. 10

    How to mock springSecurityService in an unit test

  11. 11

    How to mock this unit test in Python?

  12. 12

    writing a test using moq unit testing framework

  13. 13

    Using Moq to unit test but the object returns Null

  14. 14

    IdependencyResolver is always null in unit test using moq

  15. 15

    How to check if a function was called in a unit test using pytest-mock?

  16. 16

    How to add an item to a Mock DbSet (using Moq)

  17. 17

    Redis unit test using redis mock

  18. 18

    Mocha Unit Test Error Handling from Library

  19. 19

    How to write unit test for private method in c# using moq framework?

  20. 20

    Mock same method in a test using Moq with different linq expressions

  21. 21

    How to android unit test and mock a static method

  22. 22

    how to mock $window to unit test AngularJS service?

  23. 23

    How to mock AngularFire 2 service in unit test?

  24. 24

    How to mock the UserAgent property for an HttpRequest in a unit test?

  25. 25

    How to unit test this function without mock asserts?

  26. 26

    How to mock browserHistory in unit test environment?

  27. 27

    How to mock multiple components in camel unit test?

  28. 28

    How to mock an Akka Actor to Unit Test a class?

  29. 29

    How to mock a generic parameter for a unit test in Java?

HotTag

Archive