From: Unit Test ASP.NET WebForm on
Ok, so I may be guilty in not properly focusing my question here. My
original question quickly turned into the equivalent of the Monday Morning
Quarterback cliche'. For those who recommended VS Team System, thank you. I
will be evaluating web tests via that tool. However, I'm still soliciting
some conversation regarding the actual question here: The UnitTesting
framework and usage of the PrivateObject class in ASPX files.

I'm looking for people familiar with the UnitTesting framework and the use
of the PrivateObject class to access aspx code behind classes. I realize
that unit testing is traditionally applied to the business layer which has
better places to be than in a aspx, but Microsoft enabled unit testing of
aspx files so I think this makes it a fair question.

If you are new to this thread please look at my original post and if you
have any experience using the PrivateObject class with a ASPX webform then
perhaps you can help me. Has anyone had any luck evaluating a user control
via a unit test on a aspx via the PrivateObject or some other approach?

"Unit Test ASP.NET WebForm" wrote:

> Alright, so I'm trying to do the right thing and bring the concept of Unit
> Testing into our ASP.NET Web Application, but we're running into walls left
> and right. The biggest challenge we're facing right now involves unit
> testing a .ASPX page where the assertion needs to evaluate a property of a
> .ASCX user control included on this page. My thought was that we could
> construct a PrivateObject the same way we do for a ASPX and be able to use
> the po.GetProperty method to retrieve a property value at run-time for the
> user control to test the assertion. However, we receive the
> System.MissingMethodException exception when attempting to access this
> property that is defined in the code-behind of the .ASCX control. Is it
> possible to accomplish what I am trying or is some other method necessary?
>
> Here is an example of the Test Method code:
>
> <TestMethod()> _
> <HostType("ASP.NET")> _
> <UrlToTest("http://localhost/test/MyProspectsDetail.aspx")> _
> <AspNetDevelopmentServerHost("C:\Projects\Test\Source", "/test")> _
> Public Sub MyTest()
> 'System.Diagnostics.Debugger.Break()
> Dim p As Page = TestContext.RequestedPage
> Dim po As PrivateObject = New PrivateObject(p)
> Dim mnu As UserControl =
> p.Master.FindControl("cphSFA").FindControl("PageMenu")
> Dim pp As PrivateObject = New PrivateObject(mnu)
> pp.GetProperty("ItemEnabled", New Object())
> 'Additional code would normally follow here, but the exception is
> encountered above and additional code wouldn't be reached, anyway. Also, no
> assignment coded above intentionally to point out the exception, otherwise
> the variable would just receive a value of "Nothing".
> Assert.IsFalse(True, "Test")
>
> End Sub
>
> Any thoughts?
>
From: Patrice on

>> However, we receive the
>> System.MissingMethodException exception when attempting to access this
>> property that is defined in the code-behind of the .ASCX control. Is it
>> possible to accomplish what I am trying or is some other method
>> necessary?

What if you start by trying a property exposed by the UserControl class ?

If it works, what if you try to use the actual type of your user control
instead of UserControl or use the
http://msdn.microsoft.com/en-us/library/ms243390(VS.80).aspx
constructor ?

For now I would suspect that privateobject doesn't inspect the actual type
but the type used for declaring the argument ?

--
Patrice