I want to write some unit tests for an ASP.NET WebForms app and I get a MissingMemberException when I try to invoke a private method from the page (private bool MyPrivateMethod()): System.MissingMethodException: Method 'MyPrivateMethod' not found
If I write simple Reflection code for this method, the method gets called (a BP gets hit).
Type t = typeof(MyPage);
var m = t.GetMethod("MyPrivateMethod", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
// var i = Activator.CreateInstance(t);
// m.Invoke(i, null); // <- this also works
m.Invoke(TestContext.RequestePage, null);
I will try this but I don't like the note: "The use of Accessors has been deprecated in Visual Studio 2010 and might not be included in future versions of Visual Studio."
eusebiu
Member
137 Points
93 Posts
UnitTesting an ASP.NET WebForms page
Apr 27, 2012 04:30 PM|LINK
Hey guys,
I want to write some unit tests for an ASP.NET WebForms app and I get a MissingMemberException when I try to invoke a private method from the page (private bool MyPrivateMethod()): System.MissingMethodException: Method 'MyPrivateMethod' not found
The unit test method looks like this:
[TestMethod] [UrlToTest("http://localhost:25153/MyPage.aspx")] [HostType("ASP.NET")] [AspNetDevelopmentServerHost(SITE_LOCAL_PATH, SITE_ROOT)] public void When_populating_the_grid() { try { System.Diagnostics.Debugger.Break(); PrivateObject po = new PrivateObject(TestContext.RequestedPage); object response = po.Invoke("MyPrivateMethod", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); Assert.IsTrue(Convert.ToBoolean(response)); } catch (Exception ex) { throw ex; } }If I write simple Reflection code for this method, the method gets called (a BP gets hit).
Type t = typeof(MyPage); var m = t.GetMethod("MyPrivateMethod", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); // var i = Activator.CreateInstance(t); // m.Invoke(i, null); // <- this also works m.Invoke(TestContext.RequestePage, null);What am I missing?
Thanks
Ken Tucker
All-Star
16797 Points
2608 Posts
MVP
Re: UnitTesting an ASP.NET WebForms page
Apr 29, 2012 10:03 AM|LINK
I would use this method instead of reflection to test private methods
http://msdn.microsoft.com/en-us/library/bb385974.aspx
Space Coast .Net User Group
eusebiu
Member
137 Points
93 Posts
Re: UnitTesting an ASP.NET WebForms page
Apr 29, 2012 12:01 PM|LINK
Hey,
I will try this but I don't like the note: "The use of Accessors has been deprecated in Visual Studio 2010 and might not be included in future versions of Visual Studio."
Thanks.
Ken Tucker
All-Star
16797 Points
2608 Posts
MVP
Re: UnitTesting an ASP.NET WebForms page
Apr 29, 2012 12:09 PM|LINK
http://stackoverflow.com/questions/9174730/unit-testing-private-method
Space Coast .Net User Group