Testing code by precompiling view pages in asp.net mvc#

I am loving to use ASP.NET MVC at work and have several projects that we have live right now.  We are also migrating our web forms app very slowly by adding more mvc like views in our web forms so they can be ported easier down the road.  The one issue I am having so far is testing the code that is in the pages for the mvc apps and the code in the web form views.  Runtime errors are so annoying and when on a decent size team, it is almost impossible to test every page at runtime, especially when you don't always know what another developer added or changed. 

So I wrote a test that precompiles the entire app and all the code in the aspx page.  It is more of a sanity check than anything and has been very helpful.  It has caught several simple errors that would have ended up in production and am very thankful for having this sanity test.  Here is the sample test code that you can add to your test project:

        const string ASPNET_COMPILER_PATH = "C:/Windows/Microsoft.NET/Framework/v2.0.50727/aspnet_compiler -p {0} -v {1}";
        const string TERMINAL_APP_NAME = "cmd.exe";

        [TestMethod]
        public void Precompile_all_pages()
        {
            string sitePath = @"D:\websites\mywebsite.com\preview";
            string siteNode = "preview.mywebsite.com";

            string ret = compileProject(sitePath, siteNode);

            string msg = "Utility to precompile an ASP.NET application\r\nCopyright (C) Microsoft Corporation. All rights reserved.\r\n\r\n";
            Assert.AreEqual(msg, ret);


        }

        public string compileProject(string compileDir, string iisApplicationPath)
        {
            string compileString = string.Format(ASPNET_COMPILER_PATH, compileDir, iisApplicationPath);

            Process proc = new Process();
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.FileName = TERMINAL_APP_NAME; //File name to execute
            proc.StartInfo.Arguments = "/c " + compileString;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardInput = true;
            proc.Start(); // Execute process
            proc.WaitForExit();

            string output = proc.StandardOutput.ReadToEnd();

            return output;
        }
Make sure to reference System.Diagnostics.  You can also add the aspnet_compiler to your post build in your cproj file, although this will slow down your building of your app locally, but is a good practice I think for working with mvc.
Saturday, August 30, 2008 8:12:47 AM UTC #    Comments [1]  |  Trackback

 

Monday, May 24, 2010 1:32:58 PM UTC
For all I know this could be a really old entry, since there are no date marker on the article. In that case I apologize.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, i, strong, u)  

Enter the code shown (prevents robots):

 

All content © 2010, Jim Zimmerman
Book
New Book
Links to me
On this page
Sponsors
Calendar
<August 2008>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456
Archives
Tags
Blogroll OPML
Technorati
Favorite Links
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail