vanderkleij/Smocks

Is it error? Smocks throwing Could not load type System.TypeLoadException

SunMaungOo opened this issue · 1 comments

I have try to use Smocks in a static method but it is throwing System.TypeLoadException exception. I have added the stack trace below. Is it the error on smocks or the error on my code? Could you please advise me the step I need to perform (if the error in on my application)?

The class that I am trying to mock is something like this.

public class MyClass
{
        private MyClass()
        {

        }

        public static ObservableCollection<Data> Foo(string name, ref    List<SqlParameter> parameters, out System.Exception exception)
       {
                    ObservableCollection<Data> dataList=new ObservableCollection<Data>();

                   //operation code
            
                 return dataList;
       }

}

Result StackTrace:

at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)
   at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
   at System.Reflection.Assembly.GetType(String name, Boolean throwOnError)
   at Smocks.IL.Resolvers.AssemblyTypeContainer.GetType(String name)
   at Smocks.IL.Resolvers.TypeResolver.Resolve(TypeReference type, GenericBindingContext bindingContext)
   at Smocks.IL.Resolvers.MethodResolver.ParametersMatch(MethodBase method, TypeReference[] parameterTypes, GenericBindingContext bindingContext)
   at Smocks.IL.Resolvers.MethodResolver.<>c__DisplayClass5_0.<FindMethod>b__2(MethodBase method)
   at System.Linq.Enumerable.<>c__DisplayClass6_0`1.<CombinePredicates>b__0(TSource x)
   at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at Smocks.IL.Resolvers.MethodResolver.FindMethod(IEnumerable`1 candidates, String name, TypeReference[] parameterTypes, Boolean isGeneric, GenericBindingContext bindingContext)
   at Smocks.IL.Resolvers.MethodResolver.Resolve(MethodReference methodReference, GenericBindingContext bindingContext)
   at Smocks.IL.Resolvers.MethodResolver.Resolve(MethodReference methodReference)
   at Smocks.IL.ILGeneratorInstructionVisitor.VisitInlineTok(Instruction instruction, MethodReference methodReference)
   at Smocks.IL.InstructionExtensions.VisitInlineTok[T](Instruction instruction, IInstructionVisitor`1 visitor)
   at Smocks.IL.InstructionExtensions.Accept[T](Instruction instruction, IInstructionVisitor`1 visitor)
   at Smocks.IL.DynamicMethodCompiler.AddInstructions(IILGenerator generator, IEnumerable`1 instructions, Dictionary`2 locals)
   at Smocks.IL.DynamicMethodCompiler.Compile[T](TypeReference[] parameters, IEnumerable`1 instructions, IEnumerable`1 variables)
   at Smocks.IL.ExpressionDecompiler`1.Decompile(MethodBody body, Instruction instruction, Object target, Int32 expectedStackSize, Int32 stackEntriesToSkip)
   at Smocks.IL.ExpressionDecompiler`1.Decompile(MethodBody body, Instruction instruction, Object target)
   at Smocks.IL.SetupExtractor.<GetSetupsFromInstructions>d__10.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Smocks.Smock.CreateAssemblyRewriter(Delegate delegate, Configuration configuration)
   at Smocks.Smock.RunAction(Action`1 action, Configuration configuration)
   at Smocks.Smock.Run(Configuration configuration, Action`1 action)
   at Smocks.Smock.Run(Action`1 action)
   at MyProjectTest.UnitTestSearch.TestSmocks() in C:\Users\Admin\Documents\Visual Studio 2015\Projects\MyProject\MyProject\MyProjectTest\UnitTest1.cs:line 26
Result Message:	
Test method MyProjectTest.UnitTestSearch.TestSmocks threw exception: 
System.TypeLoadException: Could not load type 'System.Collections.Generic.List`1<System.Data.SqlClient.SqlParameter>' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

It seem Smocks cannot handle "ref" in the parameter.

After I change the method signature to

public static ObservableCollection<Data> Foo(string name,List<SqlParameter> parameters, out System.Exception exception) , it is now working as expected.