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've found that if an instance created several times, than its expectations do not work.

Note the code below. It will fail - "Init" will be actually mocked just once instead of requested 3 and thus NotImplementedException will be thrown. The same with ExpectAlways(). If you set int count = 1 then it's ok.

namespace ClassLibrary1
{
    using System;
    using NUnit.Framework;
    using TypeMock;

    public class Class1
    {
        public static Class1 Instance
        {
            get
            {
                Class1 cl = new Class1();
                cl.Init();
                return cl;
            }
        }

        private Class1()
        {
        }

        public void Init()
        {
            throw new NotImplementedException();
        }
    }

    [TestFixture]
    public class Class2
    {
        [Test]
        public void Test()
        {
            int count = 3;

            Mock mock = MockManager.Mock(typeof (Class1));
            mock.ExpectCall("Init", count);

            Class1 ins;

            for (int i = 0; i < count; i++)
            {
                ins = Class1.Instance;
            }
        }
    }
}
asked by ezaritov (2.8k points)

3 Answers

0 votes
Hi,
You should use MockAll or use Mock 3 times (see examples below).

:idea: The reason for this is that:
each Mock controls ONE instance of the Type.
MockAll controls ALL instances.
For more information see Understanding Instance Mocking in the User Guide.

Here is the detailed test:
[Test]
public void Test()
{
    int count = 3;

    // mock ALL instances of Class1
    Mock mock = MockManager.MockAll(typeof (Class1));
    mock.ExpectCall("Init", count);
    // could also use: mock.ExpectAlways("Init") 
    //   if the number of times in not important

    Class1 ins;
    for (int i = 0; i < count; i++)
    {
        ins = Class1.Instance;
     }
} 


or you could use the following:
[Test]
public void Test()
{
    int count = 3;

    // mock 3 instances of Class1
    for (int i = 0; i < count; i++)
    {
        Mock mock = MockManager.Mock(typeof (Class1));
        mock.ExpectCall("Init");
     }   

    Class1 ins;
    for (int i = 0; i < count; i++)
    {
        ins = Class1.Instance;
     }
} 
answered by scott (32k points)
0 votes
Thank you, Scott. Though I read this topic before starting using TypeMock.NET, it looks like I read it inaccuratly. Now everything in my code seems to work correctly and as expected. Your quick and helpfull answer is just one more point to by your product.
answered by ezaritov (2.8k points)
0 votes
Thanks :-)
You could also use the TypeMock.NET Visual Tracer.
This would show you that your instances are not mocked.
answered by scott (32k points)
...