-
Notifications
You must be signed in to change notification settings - Fork 14
Precompiling your Web Application
The core of what makes the WebFormsTest harness unique is that you can precompile your application in your tests and verify that ALL of your code, including the .NET code that is included in the ASPX / ASCX files executes properly.
When an ASP.NET application is compiled run on a web server, the entire application is not compiled and delivered, only the class files. During the first request to an application, the web service will perform a pre-compilation task that merges the ASPX and ASCX files with their code-behind files. This merge operation has traditionally made Web Forms applications extremely hard to test.
Typically when a unit-test is written, the test should run with as little interference from outside sources as possible. This means we don't want to depend on a database, filesystem, network, or any external dependency. The web service is another of those dependencies that our tests should not rely on. The WebFormsTest harness circumvents this requirement by providing the WebApplicationProxy static class that will serve as our web service host process. We can initialize this class with a pointer to our web application source code and then retrieve pages from the web application that are 'fully hydrated' - that is they are merged with their ASPX / ASCX code and contain a complete reference to the entire control tree that was declared in those files.
Lets take a look at a sample test to inspect the controls on a default.aspx page that came from the standard ASP.NET 4.6 web application templates. You can examine the sample code for this demo from the /Samples/FirstPrecompiled folder.
- Create a new web application in Visual Studio with Web Forms. Be sure to check the box for "Add Unit Tests"
- In the resultant code, install the WebFormsTest harness in both the test and web projects by executing the following command in the package manager console:
Install-Package WebFormsTest -ProjectName <YOUR TEST PROJECT NAME>- You will also need to add to your test project any package references your web project is using. In particular, you will want to install the Microsoft.AspNet.Web.Optimization.WebForms package.
Install-Package Microsoft.AspNet.Web.Optimization.WebForms -ProjectName <YOUR TEST PROJECT NAME>At this point, you can start testing your web application by writing some code to start up a WebApplicationProxy for some object type that is present in the Web Forms project:
WebApplicationProxy.Create(typeof(MyWebApplication._Default));This creates a WebApplicationProxy that will act as a pseudo-webserver for your code and allow you to query it for pages in the application. We can request an instance of that default page with the following syntax:
var myPage = WebApplicationProxy.GetPageByLocation<MyWebApplication._Default>("/default.aspx");... and now you have a concrete instance of a page that is ready to be "served". You can inspect any properties of the page, and you can trigger page life-cycle events with the RunToEvent method:
myPage.RunToEvent(WebFormEvent.Load);