nsubstitute / NSubstitute

A friendly substitute for .NET mocking libraries.
https://nsubstitute.github.io
Other
2.67k stars 262 forks source link

Serialising substitutes #48

Open dtchepak opened 13 years ago

dtchepak commented 13 years ago

Reported on the mailing list.

using NSubstitute;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SerializeNSubstitute
{
       class Program
       {
               static void Main(string[] args)
               {
                       var root = Substitute.For<Root>();
                       var formatter = new BinaryFormatter();
                       Root newRoot = null;

                       using(var stream = new MemoryStream())
                       {
                               formatter.Serialize(stream, root);
                               stream.Position = 0;
                               newRoot = formatter.Deserialize(stream) as Root;
                       }
               }
       }

       [Serializable]
       public abstract class Root
       {
               public abstract string Data { get; set; }
       }
}

When I run this, I get the following error (happens at the Serialize() call):

System.MethodAccessException was unhandled Message=Attempt by method 'Castle.Proxies.RootProxy.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)' to access method 'Castle.DynamicProxy.Generators.Emitters.TypeUtil.Sort(System.Reflection.MemberInfo[])' failed. Source=DynamicProxyGenAssembly2 StackTrace: at Castle.Proxies.RootProxy.GetObjectData(SerializationInfo , StreamingContext ) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) at SerializeNSubstitute.Program.Main(String[] args) in G: \JasonBock\Personal.NET Projects\SerializeNSubstitute\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

I looked at the embedded Castle.DynamicProxy stuff in the NSubstitute DLL and the Sort() method exists, but this is occuring in the proxy itself, so I'm not sure what's going on there.

kkozmic commented 13 years ago

can that be reproduced using just DP?

dtchepak commented 13 years ago

Confirmed this is not a DP problem. Following code runs fine using DynamicProxy directly:

    public class Class1 {
        [Test]
        public void TestName() {
            var gen = new ProxyGenerator();
            var root = gen.CreateClassProxy<Root>(new DoNothingInterceptor());

            var formatter = new BinaryFormatter();
            Root newRoot = null;

            using (var stream = new MemoryStream()) {
                formatter.Serialize(stream, root);
                stream.Position = 0;
                newRoot = formatter.Deserialize(stream) as Root;
            }
        }
    }

    [Serializable]
    public class DoNothingInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { } }

    [Serializable]
    public abstract class Root { public abstract string Data { get; set; } }