Visual Studio Unit Test From Existing ASP Web Site

190 views Asked by At

Denizens of Stack Overflow, I come before you in hopes of solutions to my current problem, as so many of my questions have been answered by you veritable founts of knowledge. Is there a simple way to create a unit test for an ASP Web Site Project that was already created without requiring the installation of software that requires you to buy them should you wish to continue using them? If there was a way to get the ASP Web Site directory to be treated as a Project, that would solve things very smoothly. There are two methods to accomplish this that might still be viable but that I have given up on are:

  1. Linking the ASP Website Project to a normal Visual Studio Project.
  2. A method I saw online suggested that one could simply drag the ASP.Net Web Site Folder on to a normal VS Project and this would effectively make the Project a copy of the ASP Web Site with all functionality of the Web Site for Unit Testing with the sublime easiness of being able to use Add Reference for the Unit Test Project, so much more simple than what I've encountered with Add Service/Web Reference. On a similar note, there was a website that suggested adding all the content of the ASP Web Site into the Root of a project. Neither of them worked for me, but I might have been made a mistake in my interpretation of the instructions.

Once I gave up trying to get the Unit Test to Add Reference to the ASP Web Site, my next approach was to link a Web Reference to the Unit Test. At first I tried placing the http://localhost(number)/ of the ASP Web Site in the Web Reference URL, but that didn't work. I saw something that mentioned creating an IIS Site for the Unit Test to reference, but I couldn't make much sense of it.

I've been trying to come up with a wsdl file, and to that end I downloaded WCF LOB Adapter 2013. I don't have BizTalk installed, and after the software provided the message that BizTalk needed for BAM to be installed, I decided against downloading further software. BizTalk itself was already about 660 Megabytes, and for something that is easily a fifth the size of the entire Windows 10 Operating System, I thought that BizTalk ought to be able to run without needing additional specialized software.

I recently discovered that .asmx pages happen to show up when I tried to Add a Web Reference. Instead of giving me an error about connectivity, I received an error stemming from how I had duplicate web.config files in the same solution thanks to trying so many different approaches without success and not clearing them out.

I haven't really looked much into MVCs because the tutorials I go through don't really show how you use MVCs to test the existing code of one of the various aspx pages that my company wants to have Unit Tested.

I think I was having success with [this][2] tutorial, but trying to run a asmx file I created gave me the "Not well formed" error, and my Command Prompt doesn't recognize me as administrator, even though Control Panel says I have admin privileges, which seems to be a large impediment to applying the different work-arounds. However, after using "Clean Solution" several times and deleting other projects from the solution, I was able to run the asmx file after all. However, now I'm stuck on the end of the 2nd step where a batch file (.bat) is supposed to be creating a .cs file in the Bin folder from the .asmx page. I've gotten to where the Visual Studio Developer Command Prompt is okay with both 'wsdl' and 'wsdl.exe' no longer give the: wsdl/wsdl.exe "is not recognized as an internal or external command, operable program, or batch file" error.

The contents of my batch file are:

wsdl /l:CS /n:WService /out:bin/wsdlWalkthrough.cs
http://localhost/webserv.asmx?wsdl

Since I'm trying to follow the steps of an article old enough to have graduated elementary school, the syntax may easily have changed. Instead, now I get the error:

'http:' is not recognized as an internal or external command, operable program, or batch file.

I'm going to make this one into a question in and of itself, because this question was if there was a smooth, simple, cost-free way of setting up a Unit Test for an ASP.Net Web Site, not specifically to answer any of my problems, though such would be received with much gratitude.

[Another tutorial][3] might yet provide me a solution, but I'm not going to hold my breath.

UPDATE 2/29/2016 With assistance from Stack Overflow's ever so helpful ChristiFati, I was able to get through Dimitrios Markatos' Creating and Consuming .NET Web Services in 5 Easy Steps which can be found at http://www.sitepoint.com/net-web-services-5-steps-3/ The article may be over a decade old, but it was still by far the easiest method I came across in my week or two of trying to figure out a way to add a reference, or in this case Add Web Reference that I came across. Two things to be careful of though. The first is that if you copy and paste code directly from the tutorial, you might end up with errors because the batch files you make will have an extra newline character, which caused the Developer Command Prompt for VS2012 I was using to run the .bat file to give me the

'http:' is not recognized as an internal or external command, operable program, or batch file.

error. Thanks again to ChristiFati for pointing that out for me. Similarly, the instructions for Step 3 give you the following code for a second .bat file:

csc /t:library /out:binGetSuppliers.dll binGetSuppliers.cs      
  /reference:System.dll,System.Data.dll,System.Web.dll, System.Web.Services.dll,System.XML.dll /optimize 

Besides eliminating the white-space and newline characters, the above code should probably look more along the lines of:

csc /t:library /out:bin/GetSuppliers.dll bin\GetSuppliers.cs      
  /reference:System.dll,System.Data.dll,System.Web.dll, System.Web.Services.dll,System.XML.dll /optimize 

But aside from that, the tutorial was a gift from God after all the dead ends I had come across. I'm judging my question answered because the critical issue was being able to give my Unit Test a Reference, whether a normal reference or Web Reference. I do not anticipate any more major difficulties and hopefully I will be able to finally proceed to Unit Testing for one of our ASP Web Sites. If not, this post will be edited to describe the newest problem.

Well, this is ridiculous. I'm able to add the Web Reference just fine, but doing so seems to have done diddly-squat for being able to reference the different .cs pages anywhere inside the ASP.NET Web Site. Does anyone have a way to get a Unit Test to be able to reference code/classes within an ASP.NET Web Site?

UPDATE 3/8/2016 There is a somewhat simple solution to the problem in placing all the functionality of your website into a DLL. Once that is done, right-click on the Unit Test project and select Add Reference. The DLL didn't show up on any of the tabs, but I was able to select the appropriate DLL by clicking on Browse.

Still, if there's any other ways to set up Unit Tests from an existing ASP Web Site where such a DLL doesn't exist.

My Visual Studio 2013 Solution

[2]: http: //www.sitepoint.com/net-web-services-5-steps- 2/ [3]: https: //msdn.microsoft.com/en-us/library/ms731835%28v=vs.100%29. aspx

0

There are 0 answers