Click here to Skip to main content
15,886,018 members
Articles / DevOps / Unit Testing
Tip/Trick

Unit Test Business Logic in Web Site Project

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
14 Jan 2014CPOL2 min read 12.6K   3   1
This tip explains how to perform unit testing for business logic written in App_code for a website project.

Introduction

This page aims at understanding the difficulties that we encounter while unit testing web site project. Details included in this page are intended for beginner level programmers and it is assumed that the reader has ground level knowledge on unit testing.

Background

Visual Studio makes the developer's life easy, in fact lazy by introducing "click to go" features, creating test method is one such cool feature. Now the question is "Is it possible to use VS create test method option for custom class methods in website project?". If you haven't tried this, the answer is a NO. Though VS creates test methods using a bunch of test attributes like testmethod, hosttype, UrlToTest... it can't refer to the classes declared in App_code folder. Let's see how to get rid of this problem.

Description

For a developer, the decision of making a web site as a web application project or a web site project plays a vital role. Both of these options follow a different strategy during execution. But what makes the class methods inside App_Code folder in web site project to be treated as special?

"Web application project compilation generates a DLL in Bin folder, whereas in case of web site project, there wont be any DLL generation."

After reading the above statement, you must have understood the reason behind our problem. Yes, your guess is correct, since there is no DLL generated for the classes in App_Code folder, Visual Studio can't refer to those class names from the unit test project.

Apart from normal class methods, even the web service methods definitions are often placed inside App_Code folder. In such situations, it is very essential to unit test the business logic written for custom classes and web service methods.

Solution

Though the solution for this problem is simple, it is non-trivial and a must learn concept. Follow the below steps to resolve this problem.

  1. Right click the web site project and click on 'publish web site' .
  2. Give the target location as one of your folders in hard-drive and click on Ok.
  3. Create a new test project in the solution and try to add reference to the test project.
  4. In the reference dialog box, go to the path given in step-2 and select App_Code.dll.
  5. Voila, that's it!! From your test project, now you can refer to all the classes present inside App_code folder.
  6. From here on, the traditional test method creation steps follows.

I am leaving the remaining steps to the reader as the rest of the process is trivial to the current context.

Points of Interest

App_Code folder in web site project is not a simple folder, it has got special importance in ASP.NET. The code inside this folder is compiled at site run time. Hence any file inside App_Code can be replaced in production server without pre-compilation.

It is worth noting that for this very same reason, App_Code folder is not available for web application project type as it would cause conflicts (compile time DLL and run time DLL).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I am a programmer, trekker, foodie and tennis/cricket follower . Over the last 7 years, I am working as a developer for web based ordering systems and web services in service oriented architecture(SOA) using various Microsoft .NET technologies. My core skills include ASP.NET,MVC, WCF, jQuery, C# and C++.

Comments and Discussions

 
QuestionNo App_Code.dll Pin
sandeegk7723-Jan-18 5:33
sandeegk7723-Jan-18 5:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.