Infosys Microsoft Alliance and Solutions blog

« Querying user details from Active Directory | Main | ASP.NET Unit Testing on IIS »

ASP.NET Unit Testing issue on IIS with VS 2008

I was recently trying to dirty my hands on using the Unit test framework that comes with Visual Studio. I decided to give it a try on my newly installed VS 2008. I created a pretty trivial ASP.NET site hosted on IIS with a Calculator class that I put in App_Code folder and generated the tests.

One of the key differences in working with Unit test framework for ASP.NET as compared to other projects like Winform or Class library is that the code in ASP.NET gets compiled dynamicall into various assemblies and hence we can't bind to a specific assembly upfront. Check more details on this here.

Hence to successfully test code in ASP.NET App_Code folder, the creation of private accessor is very important. However when the test case got generated, I saw the following code in the test method

        /// <summary>

        ///A test for Subtract

        ///</summary>

        [TestMethod()]

        [HostType("ASP.NET")]

        [UrlToTest("http://localhost/GenericWebApp")]

        public void SubtractTest()

        {

            // Creation of the private accessor for 'Microsoft.VisualStudio.TestTools.TypesAndSymbols.Assembly' failed

            Assert.Inconclusive("Creation of the private accessor for \'Microsoft.VisualStudio.TestTools.TypesAndSy" +

                    "mbols.Assembly\' failed");

        }

And I didnt' see any private accessor created for me. I opened the Calculator class code (from the App_Code) folder and from the context menu invoked the "Create Private Accessor" command, but got a message stating that "Private accessor creation failed for http://localhost/GenericWebApp". This was strange and needless to say that I wasn't able to run the tests.

While creating the tests, I had been prompted that Network Service didn't had sufficient rights on a particular folder and I had selected "Yes" to provide those rights. But still things didn't work. I then created an exact same ASP.NET Web site but this time on file folder. Again I created the tests and this time it worked like a breeze. See the code that got generated for one of the methods in the Calculator class.

        /// <summary>

        ///A test for Subtract

        ///</summary>

        [TestMethod()]

        [HostType("ASP.NET")]

        [AspNetDevelopmentServerHost("%PathToWebRoot%\\FileSystemWebApp", "/FileSystemWebApp")]

        [UrlToTest("http://localhost/FileSystemWebApp")]

        public void SubtractTest()

        {

            Calculator_Accessor target = new Calculator_Accessor(); // TODO: Initialize to an appropriate value

            int x = 0; // TODO: Initialize to an appropriate value

            int y = 0; // TODO: Initialize to an appropriate value

            int expected = 0; // TODO: Initialize to an appropriate value

            int actual;

            actual = target.Subtract(x, y);

            Assert.AreEqual(expected, actual);

            Assert.Inconclusive("Verify the correctness of this test method.");

        }

Clearly the private accessor (Calculator_Accessor) got created this time and after commenting the last Assert.Inconclusive line, I was able to successfully run the tests (the test would be more meaningful after making suitable changes to the values as well like x could be set to 10, y to 5 and hence expected will be 5).

This meant there was some issue in the way the tests were generated on IIS. I also did ensure that I was running VS as administrator (since I was working on Vista). I hence raised a query on the forum and got a confirmation that this is a bug in VS 2008 and we should get a fix in SP1. Till then, we will have to either test by having the site on a file folder or using the PrivateObject to test. I will discuss that in my next blog, so keep watching !

 

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter