Running in System.MissingMethodException: method not found with PrivateObject

Basically, some of my tests succeed, some of them fail. Mr. Skeet is a great suggestion, I created a complete sample code to confirm that I am not crazy. This is the code:

namespace ClassLibrary { using System; public class Manager { private int SampleMethod(int id) { return id; } } } 

My test:

 namespace UnitTestProject { using System; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class UnitTest { [TestMethod] public void TestPasses() { var privateInfo = new PrivateObject(new ClassLibrary.Manager()); var actual = privateInfo.Invoke("SampleMethod", 1); } [TestMethod] public void TestErrorsOut() { var privateInfo = new PrivateObject(new ClassLibrary.Manager()); var actual = privateInfo.Invoke("SampleMethod", 0); } [TestMethod] public void TestWorksAsWell() { var privateInfo = new PrivateObject(new ClassLibrary.Manager()); privateInfo.Invoke("SampleMethod", new object[] { 0 }); } [TestMethod] public void TestAlsoErrorsOut() { var privateInfo = new PrivateObject(new ClassLibrary.Manager()); var types = new Type[] { typeof(int) }; var actual = privateInfo.Invoke("SampleMethod", types, 0); } } } 

The first test (TestPasses ()) works.

The second test (TestErrorsOut ()) fails with the following error: {"The method" ClassLibrary.Manager.SampleMethod "was not found." }

The incomprehensible thing is that the error is consistent, but the actual test is almost identical. It makes no sense. I tried this on VS2012 RC and VS2010 with the same results.

The only thing I can think of is “0”, because it gets like something other than int, which means that it cannot find the signature of the SampleMethod method? I tried the third test to explicitly pass the type I'm looking for (TestAlsoErrorsOut ()), but this also leads to errors with the same error.

Ideas? Thank you

Edit to add

Using Ian's suggestion of using obj [] instead of overloading params obj [], it works (test TestWorksAsWell ()). And this explains why TestAlsoErrorsOut () fails because I use the params method, which will not work with Type []. So this has been fixed. But why? Why do params obj [] work when passing 1, but not 0?

+6
source share
2 answers

According to the docs ( http://msdn.microsoft.com/en-us/library/ms243710.aspx ) the arguments should be passed as an array of objects. Explicitly passing an array of objects that works correctly:

 var actual = (int)privateInfo.Invoke("SampleMethod", new object[] {0}); 

Passing 0 by itself causes the compiler to select this overload

 Invoke(string name = "SampleMethod", System.Reflection.BindingFlags bindingFlags = Default, object[] args = {object[0]}) 
+9
source

I got a System.MissingMethodException when calling the following test:

 PrivateObject shadow = new PrivateObject(target); shadow.Invoke("PrivateMethod", new string[]{"arg1","arg2"}); 

In the signature of the target private method

 private void PrivateMethod(string[] args) 

I had to add params to the private method signature to eliminate the exception as follows:

 private void PrivateMethod(params string[] args) 
0
source

Source: https://habr.com/ru/post/921255/


All Articles