Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing ASP.net Web Site Project code stored in App_Code

I have an ASP.net Web Site Project (.net 3.5). Currently all of the non-code behind code files (including Linq2Sql stuff, data contexts, business logic, extension methods, etc) are located in the App_Code folder.

I am interested in introducing Unit Testing (using nunit) in at least some sections of the project moving forward. Any Unit Testing that I would be doing would need to have full access to all of the code that is currently located in the App_Code folder. I have done some initial reading so far, and the consensus seems to be:

  • This will not be possible given my current setup
  • Unit testing requires referencing classes that are part of a compiled dll, and a Web Site Project by definition only compiles at run time.
  • In order to proceed, I will need to either convert my entire project to a Web Application, or move all of the code that I would like to test (ie: the entire contents of App_Code) to a class library project and reference the class library project in the web site project. Either of these will provide access to the classes that I need in compiled dll format, which will allow me to Unit Test them.

Is this correct? Or is there another way that I can Unit Test without restructuring/refactoring my entire project?

like image 510
Yaakov Ellis Avatar asked Jul 29 '09 07:07

Yaakov Ellis


People also ask

What is store in App_Code folder?

You can store source code in the App_Code folder, and it will be automatically compiled at run time. The resulting assembly is accessible to any other code in the Web application. The App_Code folder therefore works much like the Bin folder, except that you can store source code in it instead of compiled code.


1 Answers

My shop has finally worked through an answer for this for our MVC project. And I want to share it as I chased a lot of dead ends here on StackOverflow hearing a lot of people say it couldn't be done. We do it like this:

  • Open the MVC folder "as a website, from local iis" which gets intellisense and debugging working properly
  • Add a unit test project that lives in our source controlled directory
  • Add a pre-build step to the TEST project, since we can't add one to a project that is open as a website. Imagine website is \FooSite and our test project is \FooSite.Tests. The compiled app code will end up in FooSite.Tests\FooSite_Precompiled\bin.
  • *
<Target Name="BeforeBuild">      <AspNetCompiler VirtualPath="FooSite" TargetPath="$(ProjectDir)\FooSite_Precompiled" Force="true"  Debug="true" />   </Target> 
  • Add a reference to the FooSite_Precompiled/bin/App_Code.dll in your test project.
  • Boom that's it. You can have your cake and eat it too. Every time you click Build in your solution you call the aspnet_compiler.ext tool on your website csproj (which does still exist) which is able, unlike MSBuild, to compile app_code, and the Debug="true" allows you step into the app_code.dll code when debugging your unit test. And you only need to Build when you're running updated unit tests. When you're looking at the effects of your change on the page, you just Change Code/Save/Refresh Page since the app_code folder dynamically compiles when called from your web server.
like image 195
Brian White Avatar answered Sep 24 '22 18:09

Brian White