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

Hi,
Thanks for the complete example. :)
:( Basiclly you cannot mock the ToString() of XmlDocument because this method is actually defined in the Object class, and TypeMock does not mock types from mscorlib. (Object is defined in mscorlib).
It is actually our mistake that you are not alerted of this, and we will alert these cases in the coming version, so if you will try to mock a method that is actually defined in mscorlib, you will be notified
answered by richard (3.9k points)
0 votes
I understand the limitation, that's fine

I have changed the code to a more realistic

myMock.ExpectGet("InnerXml", "<Hello />");


And the class to

if(doc.InnerXml == "<Hello />")


This works fine.

Yes, in General, a lot of time has been trying to work out what is wrong, as "1 Expected Call" seems a bit of a generalism, so more explicit error messages would be excellent.

Many thanks again
answered by robpearmain (2.8k points)
0 votes
Yes, in General, a lot of time has been trying to work out what is wrong, as "1 Expected Call" seems a bit of a generalism, so more explicit error messages would be excellent.


Hi,
This issue seems important, so we have added an error message in TypeMock version 2.2.2.0 :D
answered by richard (3.9k points)
0 votes
Ok, the following code worked in 2.2.1 and now fails the 2 mocking tests in 2.2.2

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.ExpectGet("InnerXml", "<Hello />");
         
         Assert.AreEqual(_calc.f2(5, 2), 3);
      }

      [Test]
      public void Pass5And2AndXmlDocWithGoodbyeReturnsM3()
      {
         Mock myMock = MockManager.Mock(typeof(XmlDocument));
         myMock.ExpectConstructor();
         
         myMock.ExpectGet("InnerXml", "<Goodbye />");
         
         Assert.AreEqual(_calc.f2(5, 2), -3);
      }

      [TearDown]
      public void TearDown()
      {   
         MockManager.Verify();
      }
   }
}


This is on 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.InnerXml == "<Hello />")
            return f1(x, y);
         else
            return -f1(x,y);
      }
   }
}



------ Test started: Assembly: ChristopheSQLMock.dll ------

TestCase 'ChristopheSQLMock.Test.CalculatorTest.Pass5And2AndXmlDocWithHelloReturnsM3' failed: 
   expected:<-3>
    but was:<3>
TearDown : TypeMock.VerifyException : 
TypeMock Verification: Method System.Xml.XmlDocument.get_InnerXml() has 1 more expected calls

   c:documents and settings
obpmy documentsisual studio projectschristophesqlmock	estcalculatortest.cs(49,0): at ChristopheSQLMock.Test.CalculatorTest.Pass5And2AndXmlDocWithHelloReturnsM3()
   --TearDown
   at TypeMock.Mock.Verify()
   at TypeMock.Expectations.Verify()
   at TypeMock.MockManager.Verify()
   c:documents and settings
obpmy documentsisual studio projectschristophesqlmock	estcalculatortest.cs(66,0): at ChristopheSQLMock.Test.CalculatorTest.TearDown()

TestCase 'ChristopheSQLMock.Test.CalculatorTest.Pass5And2AndXmlDocWithGoodbyeReturnsM3' failed: 
TearDown : TypeMock.VerifyException : 
TypeMock Verification: Method System.Xml.XmlDocument.get_InnerXml() has 1 more expected calls

   
   --TearDown
   at TypeMock.Mock.Verify()
   at TypeMock.Expectations.Verify()
   at TypeMock.MockManager.Verify()
   c:documents and settings
obpmy documentsisual studio projectschristophesqlmock	estcalculatortest.cs(66,0): at ChristopheSQLMock.Test.CalculatorTest.TearDown()


5 succeeded, 2 failed, 0 skipped, took 0.16 seconds.



---------------------- Done ----------------------
answered by robpearmain (2.8k points)
0 votes
Ok, the following code worked in 2.2.1 and now fails the 2 mocking tests in 2.2.2

Hi,
There shouldn't be any difference between the two versions.
But in any case the solution to your probkem, is to use MockAll (mock the type) instead of Mock (one instance)
   Mock myMock = MockManager.Mock(typeof(XmlDocument));


should be
   Mock myMock = MockManager.MockAll(typeof(XmlDocument));


The reason for this is that NUnit uses XmlDocument internally and so the constructor cannot be mocked anymore. We will enable this in future versions.
BTW, a simple way to test XML code is to pass a dummy XML and see how the code reacts, instead of mocking the XML methods :)
answered by richard (3.9k points)
0 votes
Thanks for your reply,

There are those in our company who are against mocking because of having to change code for testing with NMock (Such as providing interfaces), so I am keen to show off TypeMock, as you need change no code when testing, so if you can enable as much functionality as possible that would be great (Unfortunetely XmlDocument was used in an example we were showing aothers s mocking a concrete class, that's when we discovered this problem)

I appreciate all your hard work :D
answered by robpearmain (2.8k points)
0 votes
Many thanks.

out of interest, I will be doing a team presentation on TDD next week, and will feature TypeMock.

1. As well as corelib (ToString, GetType and GetHashTable), is there anything else not supported by TypeMock
2. Will CoreLib ever we mockable
answered by robpearmain (2.8k points)
0 votes
Many thanks.

Great to hear that it worked

out of interest, I will be doing a team presentation on TDD next week, and will feature TypeMock.

Good luck :)

1. As well as corelib (ToString, GetType and GetHashTable), is there anything else not supported by TypeMock

There are a few corner scenarios that are not supported, these will be implemented in future releases, and of course there are a few bugs that we will fix once we know about them :D
2. Will CoreLib ever we mockable

Most of the lib will not be mockable, although some of the types that are needed for testing will be implemented in the future.
answered by richard (3.9k points)
0 votes
Hello,

when I read cross this article I found the explanation for my problem but how can I work around this limitation?
My problem:
I have objects of a class which are added to a combobox.
The combobox itself uses .ToString() to show the entry. In my class I have overridden the ToString() method to show an appropriate string for the object. If this class is mocked, only "MockIxxx" is shown for every entry.
How can I get the correct strings into the combobox's listbox? I can't select entries by string, else.

Thanks for any ideas

Michael
answered by mknaup (8.5k points)
0 votes
Hi Michael,
I have objects of a class which are added to a combobox.
The combobox itself uses .ToString() to show the entry. In my class I have overridden the ToString() method to show an appropriate string for the object. If this class is mocked, only "MockIxxx" is shown for every entry.
How can I get the correct strings into the combobox's listbox? I can't select entries by string, else.

I think that you are mocking an interface (am I mistaken?)

The solution is to mock the concrete class that overrides ToString().
If you mock the class, that overrides ToString, you could just mock it as normal (as ToString() it is not defined in mscorlib),

So just mock the class, create it and pass it.
Mock mockControl = MockManager.MockAll(typeof(YourClass));
mockControl.ExpectAndReturn("ToString","Whatever");
YourClass mockedClass = new YourClass();

Now mockedClass will return "Whatever" when ToString() is called, (if the YourClass overrides ToString)
answered by richard (3.9k points)
...