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
We cannot get the "XmlDocument.ToString()" to work, it returns "1 expected call" error

TearDown : TypeMock.VerifyException : 
TypeMock Verification: Method System.Xml.XmlDocument.ToString() has 1 more expected calls


--TearDown
   at TypeMock.Mock.Verify()
   at TypeMock.Expectations.Verify()
   at TypeMock.MockManager.Verify()
   at ChristopheSQLMock.Test.CalculatorTest.TearDown() in c:\documents and settings\robp\my documents\visual studio projects\christophesqlmock\test\calculatortest.cs:line 54


This is the class
using System;
using System.Xml;

namespace ChristopheSQLMock
{
   /// <summary>
   /// Summary description for Calculator.
   /// </summary>
   public class Calculator
   {
      public Calculator()
      {
      }

      public int f1(int x, int y)
      {
         if(x>y)
            return (x-y);
         else
            return (x+2*y);
      }

      public int f2(int x, int y)
      {
         XmlDocument doc = new XmlDocument();

         if(doc.ToString() == "<Hello />")
            return f1(x, y);
         else
            return -f1(x,y);
      }
   }
}


This is the test (See the ToString bit)
using System;
using System.Data.SqlClient;
using System.Xml;
using NUnit.Framework;
using TypeMock;

namespace ChristopheSQLMock.Test
{
   [TestFixture]
   public class CalculatorTest
   {
      private Calculator _calc;

      [SetUp]
      public void SetUp()
      {
         _calc = new Calculator();
         MockManager.Init();
      }

      [Test]
      public void Pass5And2Returns3()
      {
         Assert.AreEqual(_calc.f1(5,2), 3);
      }

      [Test]
      public void Pass6And4Returns2()
      {
         Assert.AreEqual(_calc.f1(6,4), 2);
      }

      [Test]
      public void Pass4And6Returns16()
      {
         Assert.AreEqual(_calc.f1(4,6), 16);
      }

      [Test]
      public void Pass5And2AndXmlDocWithHelloReturnsM3()
      {
         Mock myMock = MockManager.Mock(typeof(XmlDocument));
         myMock.ExpectConstructor();
         myMock.ExpectAndReturn("ToString", "<Hello />");
         

         Assert.AreEqual(_calc.f2(5, 2), -3);
      }


      [TearDown]
      public void TearDown()
      {   
         MockManager.Verify();
      }
   }
}
asked by robpearmain (2.8k points)

12 Answers

0 votes
Hi Richard,

thanks for your prompt answer. The problem if I mock the concrete class is that there is no method .Object. I need the object as a parameter:

EnumeratorMock.ExpectGet( "Current", MyClassMock.Object);


This works well with an interface (where I can call MockManager.MockObject) but what about when I have a Mock instead of a MockObject?

EnumeratorMock.ExpectGet( "Current", MyClassMock);


leads to: No method ... returns TypeMock.MockObject

You would propose to create an (real) object of MyClass in the test. Is that the only way? I can't take the original class cause it has lots of dependencies and references lots of other classes not available at testing time. So I would have to build a dummy class that implements the interface but does nothing.

Any other ideas :?:

Michael
answered by mknaup (8.5k points)
0 votes
Hi,
but what about when I have a Mock instead of a MockObject?

EnumeratorMock.ExpectGet( "Current", MyClassMock);

leads to: No method ... returns TypeMock.MockObject

Dont send the mock, create the class and send it.
You would propose to create an (real) object of MyClass in the test. Is that the only way? I can't take the original class cause it has lots of dependencies and references lots of other classes not available at testing time. So I would have to build a dummy class that implements the interface but does nothing.

This is EXACTLY what you should do, and this is the power of TypeMock.
You don't have to think in the same lines as other mocking frameworks because their restrictions don't hold for TypeMock.
Create the real class (mock out all those methods that the class is dependant on) and pass that mocked class.

There is no need to worry about external dependancies, or to create interfaces, just mock out all those nasty methods, that interfere with the testing. (You would have to do this anyway even for Interface mocks, because any method that is called from the mocked interface, must have an expectation setup. Just do the same with the real object)
Mock mockControl = MockManager.MockAll(typeof(YourClass)); 
mockControl.ExpectAndReturn("ToString","Whatever"); 
// mock out all interfering methods ...

YourClass mockedClass = new YourClass(); 
EnumeratorMock.ExpectGet( "Current", mockedClass );


Of course you could always extend the Interface yourself and add the ToString method for the test. This is called building the mock manually. But this will mean that you will have to implement ALL the methods.
answered by richard (3.9k points)
...