chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I'm trying to test a class that contains a method that returns an XmlNode. During my test, I need the mocked class to do it's original behavior, but afterwards I want to verify with which arguments it was called. As such, I do a WhenCalled.CallOriginal (so that Verify can be used afterwards) on the object and then perform the verify. However, since I used CallOriginal, I cannot verify (there's an exception about an abstract class). I have tested using WillReturn (instead of CallOriginal) and that works fine, but in my test case, I need to actually call the original.

Here's a code snippet that can reproduce the error:

[TestClass]
public class TestXmlNodeMethod
{
   [TestMethod, Isolated]
   public void TestThatSucceeds()
   {
      // ARRANGE
      var expectedNode = this.GetNode();
      Isolate.WhenCalled(() => this.GetNode()).WillReturn(expectedNode);

      // ACT
      var node = this.GetNode();

      // ASSERT
      Assert.AreEqual("<Root><Child></Root>", node.OuterXml);
      Isolate.Verify.WasCalledWithAnyArguments(() => this.GetNode());
   }

   [TestMethod, Isolated]
   public void TestThatFails()
   {
      // ARRANGE
      Isolate.WhenCalled(() => this.GetNode()).CallOriginal();

      // ACT
      var node = this.GetNode();

      // ASSERT
      Assert.AreEqual("<Root><Child></Root>", node.OuterXml);
      Isolate.Verify.WasCalledWithAnyArguments(() => this.GetNode());
   }

   public XmlNode GetNode()
   {
      var doc = new XmlDocument();
      doc.LoadXml("<Root><Child></Root>");

      return doc.DocumentElement;
   }
}
asked by Christian.Merat (2.9k points)

3 Answers

0 votes
Over two weeks and no response?
answered by Christian.Merat (2.9k points)
0 votes
Sorry about not getting back to you sooner.

Isolator does not support faking abstract classes without a public c'tor. The problem here is that the internal implementation of Verify requires Isolator to fake calls, so you get this exception. This is a known issue that will be handled in a future release.

In the meanwhile, let's try to find a workaround for this. What are you trying to test? Why do you need to call the actual implementation of GetNode() in this test? Can you separate the test into two tests, one with each assert, where the test asserting the actually loaded xml uses CallOriginal(), and the test verifying GetNode() was called uses WillReturn()?

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
If the internal implementation of Verify requires a fake, then why does it work when setting a WillReturn behavior? Shouldn't it also fail?

The true context for my test is an integration test that connects to a live system via a SOAP WebService that must validate that certain calls were made with specific arguments. Your proposed workaround works, but requires me to fake part of my integration test, which obviously is not ideal.
answered by Christian.Merat (2.9k points)
...