views:

627

answers:

5

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?

+6  A: 

Your conclusions seem correct. I would vote for moving functionality into one or several class library projects, since that may open the door for reusing the same functionality in other projects as well.

Fredrik Mörk
+2  A: 

And as the OP stated it's also possible to move to a Web App project, which i would say is cleaner as well, your pages can stay in the wep app project, you will have them in 1 DLL (testable). All your business logic etc. goes in a separate class library / libraries.

Colin
+2  A: 

It looks like this is possible whilst still using App_code, but I would either move this logic out to its own class library project or change the project type to Web Application, as Fredrik and Colin suggest.

I always create my own ASP.NET projects as Web Application projects not Websites.

RichardOD
Thanks for the link. Definitely looks like an ugly solution though.
Yaakov Ellis
Yes absolutely- hence better to go down one of the more sane routes.
RichardOD
A: 

Hi there, i also have web site project and one DataContext class in it, in which i have all logic (db working with dbml file and other methods dont working with db), if i understood correctly the only way to test this class for me without changin project type to web appl project is to move logic to a separete dll, but i dont get how the methods working with database using linq can be moved to separete dll and tested, i mean before calling such methods there must be a setup method run first which establishesh a connection to server? the same way the application establishes it during runtime? and what if some methods are dependent on configuration in my .config file? how do i test these in that case? could some1 give me an explanation plz, im quite new in web dev, Great thx.

qwebek
the best way to get a question answered is to ask a question. I have had limited success asking answers. ;-) try http://stackoverflow.com/questions/ask and let me know. Be sure to format your question clearly using logical paragraphs and enough information about your situation so that you get the best results. email me again when it is posted to get my attention as I am working on something and not trolling much today.
Sky Sanders
+1  A: 

We have this issue at my company (My boss doesn't like DLLs, some rubbish about versioning...)

We have two ways round it that we use frequently:

1) Get the CI tool to do the unit testing: We use TeamCity which has a pretty tight NUnit integration, and our solution builds quick enough (and has few enough tests) for this to be a valid option.

2) Manually precompile and unit test the resulting binaries: It's perfectly possible to run the ASP.net compiler / MSBuild from the command line (as if you were doing a 'Publish' build) and just unit test the resulting binaries.

However, if you have the option of segregating the code into binaries (class libraries) or just using a web application, I'd suggest that as a better alternative.

Ed Woodcock