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
0 votes
Trying to mock an interface I found that the following line of code works well

var objectManager = Isolate.Fake.Instance<IObjectManager11>();


but the following line throws an exception

var objectManager = Isolate.Fake.Instance<IObjectManager12>();


System.TypeLoadException occurred
HResult=-2146233054
Message=Type 'Mock0000IObjectManager12' from assembly 'DynamicMockAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a0857b23fb3f20d9' contains more methods than the current implementation allows.
Source=mscorlib
TypeName=Mock0000IObjectManager12
StackTrace:
at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
InnerException:


IObjectManager12 inherits from IObjectManager11 and extends the interface by one method.
Is there really a limit for the number of methods in an interface used for mocking? How many methods are allowed?
asked by PaulAuster (680 points)

4 Answers

0 votes
Hi,

I tried it with the latest build, and couldn't reproduce the problem.

1. Can you post the two interface definitions?
2. Also which VS and Isolator are you running? If it's not the latest Isolator, download it at https://www.typemock.com/download/and try it (it maybe a fixed bug).

Let me know and we'll work it out from there.
answered by gilz (14.5k points)
0 votes
The following code demonstrates the problem using Visual Studio 2012 and Typemock Isolator 7.1.7.0.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using TypeMock.ArrangeActAssert;

namespace TypeMockException
{
    public class Args01 : EventArgs
    {
        public Args01(int param0, Guid param1, int param2) { }
        public Exception Fct0 { get { return null; } }
        public int Fct1 { get { return 0; } }
        public Guid Fct2 { get { return Guid.Empty; } }
        public int Fct3 { get { return 0; } }
        public void Fct4(Exception param535) { }
    }

    public interface Interface01
    {
        string Fct5(string param3);
        string Fct6(string param4);
        string[] Fct7();
        bool Fct8(string param5);
        void Fct9(string param6);
        void Fct10(string param7, string param8);
        void Fct11(string param9);
    }

    public class Args02 : EventArgs
    {
        public Args02(int param10, Guid param11, int param12) { }
        public int Fct12 { get { return 0; } }
        public Guid Fct13 { get { return Guid.Empty; } }
        public int Fct14 { get { return 0; } }
    }

    public delegate void Delegate01(object param536, Args03 param537);

    public class Args03 : EventArgs
    {
        public Args03(Stream param13, string param14, string param15) { }
        public Exception Fct15 { get { return null; } }
        public string Fct16 { get { return null; } }
        public Stream Fct17 { get { return null; } }
        public string Fct18 { get { return null; } }
        public string Fct19 { get { return null; } }
        public void Fct20(Exception param538) { }
        public void Fct21(string param16) { }
    }

    public class Args04 : EventArgs
    {
        public Args04(int param17, Guid param18) { }
        public Guid Fct22 { get { return Guid.Empty; } }
        public int Fct23 { get { return 0; } }
    }

    public class Class01
    {
        public Class01() { }
        public void Fct24(string param19, Guid param20, Version param21) { }
        public override bool Equals(object param22) { return false; }
        public static Class01 Fct26(string param23) { return null; }
        public static Class01 Fct27(Stream param24) { return null; }
        public Version Fct28(string param25, Guid param26) { return null; }
        public Guid[] Fct29() { return null; }
        public override int GetHashCode() { return 0; }
        public Class02 Fct30(Guid param27) { return null; }
        public bool Fct31() { return false; }
        public bool Fct32(Guid param28) { return false; }
        public void Fct33(string param29, Guid param30) { }
        public void Fct34(Guid param31, bool param32) { }
        public void Fct35(Guid param33, Class02 param34) { }
    }

    public abstract class Class02
    {
        protected Class02() { }
        public Version Fct36(Version[] param35) { return null; }
        public abstract Version Fct37(IEnumerable<Version> param36);
        protected string Fct38(params object[] param37) { return null; }
    }

    public interface Interface02
    {
        void Fct39(string param38);
        void Fct40(string param39);
    }

    public interface Interface03 : ICloneable, IComparable, Interface04, Interface05
    {
        bool Fct41 { get; }
        Interface06[] Fct42 { get; }
        Interface07 Fct43 { get; set; }
        Guid Fct44 { get; }
        Interface09 Fct45 { get; }
        bool Fct46(Type param40);
        bool Fct47(Interface03 param41);
        bool Fct48(string param42);
        int Fct49(Interface03 param43, Interface03[] param44);
        string Fct50(ref long param45, ref int param46, bool param47);
        string Fct51(long param48);
        void Fct52();
    }

    public interface Interface04
    {
        string[] Fct53 { get; }
        void Fct54();
        void Fct55();
        object Fct56(string param49);
        void Fct57(string param50, object param51);
    }

    public interface Interface05 : ICloneable, IComparable, Interface04
    {
    }

    public interface Interface06 : ICloneable, IComparable, Interface04, Interface05
    {
        Interface03 Fct58 { get; set; }
        string Fct59(ref long param52, ref int param53, bool param54);
        string Fct60(long param55);
    }

    public interface Interface07 : ICloneable, IComparable, Interface04, Interface05
    {
        int Fct61 { get; }
        bool Fct62 { get; }
        string Fct63 { get; }
        Guid Fct64 { get; }
        Interface03 Fct65 { get; }
        Guid Fct66 { get; }
        bool Fct67 { get; }
        Guid Fct68 { get; }
        int Fct69 { get; }
        Interface08[] Fct70 { get; }
        Guid[] Fct71 { get; }
        void Fct72(Interface08 param56);
        Interface08 Fct73(Guid param57);
        void Fct74(Guid param58);
    }

    public interface Interface08 : ICloneable, IComparable, Interface04, Interface05
    {
    }

    public interface Interface09
    {
        long Fct75(bool param59);
        bool Fct76(long param60);
        void Fct77();
        void Fct78(string param61);
        string Fct79();
        void Fct80(long param62);
    }

    public interface Interface10
    {
        int Fct81 { get; }
        event EventHandler<Args05> Fct82;
        Interface12 Fct83(Guid param63, Interface03 param64, string param65);
        Interface12 Fct84(Guid param66, Interface03 param67, string param68);
        bool Fct85(Stream param69);
  
answered by PaulAuster (680 points)
0 votes
Hi Paul,

Thank you for sending the repro.

We're going to investigate it since we're not sure what causes this behavior.
Meanwhile, please try the following workaround:
In your test project create a class that implements IObjectManager12.
It can be an empty implementation like so:
public class EmptyObjectManager : IObjectManager12
{
    public void Fct461(int param534)
    {
        throw new NotImplementedException();
    }
...
}


Then, fake the class instead of faking the interface:
[TestMethod]
public void TestingClassInsteadOfInterface_WorksFine()
{
    Isolate.Fake.Instance<EmptyObjectManager>();
}


*Note that the test should be written in context of a unit test project (not inside a Main method of a class).

Please let me know if it helps.
answered by alex (17k points)
0 votes
Yes, your work around seems to work. Thank you very much.
answered by PaulAuster (680 points)
...