I want to create an ASP.Net Web Application, and I want to write unit tests for it. But my unit test project can't see any of the types in my web application's App_Code directory.
(skip down past the images if you already know how to create a default webforms web application and add an app_code folder)
Open Visual Studio. Choose File->New->Project.
Choose Templates -> Visual C# -> Web. Choose ASP.Net Web Application. Choose "OK".
Choose "Web Forms". Check "Add unit tests". Click OK.
In the solution explorer, right-click on the web application and choose "Add -> Add ASP.Net Folder -> App_Code".
Add a new item to App_Code, named "Util.cs". Add a function to the file so the contents are:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.App_Code
{
public class Util
{
public static int getMeaningOfLife()
{
return 42;
}
}
}
In the Unit Testing project, change UnitTest1.cs to:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace WebApplication1.Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
int result = WebApplication1.App_Code.Util.getMeaningOfLife();
if (result != 42)
{
Assert.Fail();
}
}
}
}
Then build the project.
The solution should build. If we attempt to test, the test should succeed.
The solution fails to build with The type or namespace name 'App_Code' does not exist in the namespace 'WebApplication1' (are you missing an assembly reference?)
I looked at Unit Testing ASP.net Web Site Project code stored in App_Code, but I don't think the problem exactly matches my circumstances because they're testing a website and I'm testing a web application. Some answers specifically suggest switching to a web application, implying that it would no longer be a problem:
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.
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.
And as the OP stated it's also possible to move to a Web App project, which i would say is cleaner as well
The advice "try using a web application" doesn't apply to me, because I'm already using a web application, and it still doesn't work.
The answers also suggest "Try moving the code out to its own project", and I do expect that would work, but in my actual solution I have dozens of files in the App_Code directory. I would prefer not to perform any serious structural changes before I can establish a testing framework first.
So to bring a bit more clarity. App_Code
folder has special meaning in asp.net projects. You can put files there and asp.net will compile them at runtime and will monitor them for changes to recompile on the fly when something changes. Details are not very relevant, main thing is: if you don't need this feature - don't use App_Code
folder.
However, there is nothing magical in that folder itself. When you create it via "Add ASP.NET folder" (or just manually in web site project) - Visual Studio will set build action of files you add there to Content
, even for code files. Such files will not be compiled into resulting assembly, that is why you cannot use them from your Unit Test project, nor you can use it even from web site code itself. If you manually change build action to Compile
- it will be compiled and can be used as usual. That's all the difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With