bitblack / moq

Automatically exported from code.google.com/p/moq
Other
0 stars 0 forks source link

When calling .Callback, if method has at least 5 parameters, then Method Not Found Moq.Language.Flow.ICallbackResult exception thrown #363

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.  Create an interface with this definition:
    public interface ITest
    {
        void LogEvent5(string a, string b, string c, string d, string e);
        void LogEvent4(string a, string b, string c, string d);
        void LogEvent3(string a, string b, string c);
        void LogEvent2(string a, string b);
        void LogEvent(string a);
    }
2.  Try and mock it up in a test method.
        [TestMethod]
        public void TestMethod1()
        {
            var mockFoo = new Mock<ITest>();
            int bar = 0;
            // This one crashes in MoQ 4.0.  Has 5 parameters.
            mockFoo.Setup(x => x.LogEvent5(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback((string a, string b, string c, string d, string e) => bar++);

            // These all work fine.  Have < 5 parameters.
            //mockFoo.Setup(x => x.LogEvent4(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback((string a, string b, string c, string d) => bar++);
            //mockFoo.Setup(x => x.LogEvent3(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback((string a, string b, string c) => bar++);
            //mockFoo.Setup(x => x.LogEvent2(It.IsAny<string>(), It.IsAny<string>())).Callback((string a, string b) => bar++);
            //mockFoo.Setup(x => x.LogEvent(It.IsAny<string>())).Callback((string a) => bar++);
        }

What is the expected output? What do you see instead?

 - When calling .Callback() on the provided sample LogEvent() method, when there are 5 arguments you get an exception.  This happens only with the Moq .NET 4.0 DLL (Moq.4.0.10827.Final) with Visual Studio 2010 (Version 10.0.40219.1 SP1Rel) running the .NET framework 4.0 on an MS test projects.

What version of the product are you using? On what operating system?
 - Moq.4.0.10827.Final with VS 2010 and .NET 4.0 framework on an MSTest project.

Please provide any additional information below.
 - This error does NOT happen with using Moq .NET version 3.5 of the DLL (Moq.3.1.416.3-bin).
 - This also does NOT happen if the number of arguments in a method is less than 5 (e.g. LogEvent1, LogEvent2, LogEvent3, and LogEvent4 all work in my example).

Original issue reported on code.google.com by joejunio...@gmail.com on 22 Apr 2013 at 11:19

GoogleCodeExporter commented 9 years ago
The exception thrown, btw, is:

Test method MVC.Tests.UnitTest1.TestMethod1 threw exception: 
System.MissingMethodException: Method not found: 
'Moq.Language.Flow.ICallbackResult 
Moq.Language.ICallback.Callback(System.Action`5<!!0,!!1,!!2,!!3,!!4>)'.

Original comment by joejunio...@gmail.com on 22 Apr 2013 at 11:27

GoogleCodeExporter commented 9 years ago
Actually this may be less of an issue with MoQ than with a combination of MoQ 
and AutoFixture and use of the gac with multiple versions of Moq installed.  
Note that AutoFixture has an AutoMoqCustomization() that is useful to many 
developers for reducing test-size dramatically (they are nice complements of 
each other).

I believe the latest version of AutoFixture (as of the time of this writing) is 
compiled directly against 3.1.416.3.  That requires an assembly binding 
redirect to be setup in the MSTest project's app.config file in order to get 
AutoFixture to work properly:

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <!-- http://stackoverflow.com/questions/6505591/autofixture-and-moq-v4 -->
      <dependentAssembly>
          <assemblyIdentity name="Moq"
                          publicKeyToken="69f491c39445e920"
                          culture="neutral"/>
          <bindingRedirect oldVersion="3.1.416.3"
                          newVersion="4.0.10827.0"/>
          </dependentAssembly>
      </assemblyBinding>    
    </runtime>  

When I remove that bindingRedirect, the issue resolves itself, however 
AutoFixture breaks.

When I put that binding back, I can get both AutoFixture and Moq to work if I 
UNGAC the 3.1 version of MoQ.  I can then either reference the 4.0 via 
GAC/strongname or not, and both will play well together.

Perhaps the assembly binding redirect is somehow detecting the 3.1 version of 
MoQ in the gac and overwriting the 4.0 version with it, causing weird issues 
like the one reported here.

See this URL for more issues:
http://stackoverflow.com/questions/6505591/autofixture-and-moq-v4

This bug can probably be closed.

Original comment by joejunio...@gmail.com on 23 Apr 2013 at 12:09