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.
我想创建一个ASP。Net Web应用程序,我想为它编写单元测试。但是我的单元测试项目在我的web应用程序的App_Code目录中看不到任何类型。
Steps to reproduce
(skip down past the images if you already know how to create a default webforms web application and add an app_code folder)
(如果您已经知道如何创建默认的webforms web应用程序并添加app_code文件夹,请跳过这些图像)
Open Visual Studio. Choose File->New->Project.
Choose Templates -> Visual C# -> Web. Choose ASP.Net Web Application. Choose "OK".
打开Visual Studio。选择文件- >新建- >项目。选择模板->视觉c# ->网。选择ASP。净的Web应用程序。选择“OK”。
Choose "Web Forms". Check "Add unit tests". Click OK.
选择“Web表单”。检查“添加单元测试”。单击OK。
In the solution explorer, right-click on the web application and choose "Add -> Add ASP.Net Folder -> App_Code".
在解决方案资源管理器中,右键单击web应用程序并选择“Add -> Add ASP”。网- > App_Code文件夹”。
Add a new item to App_Code, named "Util.cs". Add a function to the file so the contents are:
向App_Code添加一个名为“Util.cs”的新项。向文件中添加一个函数,内容如下:
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:
在单元测试项目中,更改UnitTest1。cs:
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.
然后构建项目。
Expected behavior:
The solution should build. If we attempt to test, the test should succeed.
解决方案应该建立。如果我们尝试测试,测试应该会成功。
Actual behavior:
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?)
在名称空间“WebApplication1”中不存在类型或名称空间名称“App_Code”的解决方案(是否缺少程序集引用?)
Other notes
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:
我查看了存储在App_Code中的ASP.net Web站点项目代码的单元测试,但我认为这个问题与我的情况并不完全匹配,因为他们正在测试一个网站,而我正在测试一个Web应用程序。一些回答特别建议切换到web应用程序,这意味着它将不再是一个问题:
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.
但是,如果您可以选择将代码分割成二进制(类库),或者仅仅使用web应用程序,我建议您将其作为更好的选择。
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.
我要么将此逻辑移到它自己的类库项目中,要么将项目类型改为Web应用程序,如Fredrik和Colin所建议的那样。
And as the OP stated it's also possible to move to a Web App project, which i would say is cleaner as well
正如OP所说,也有可能转向Web应用程序项目,我认为它也更干净
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.
“尝试使用web应用程序”的建议并不适用于我,因为我已经在使用web应用程序了,而且它仍然不起作用。
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.
这些答案还建议“尝试将代码移到它自己的项目中”,我确实希望它能够工作,但是在我的实际解决方案中,我在App_Code目录中有几十个文件。在我可以首先建立一个测试框架之前,我宁愿不执行任何严重的结构更改。
1 个解决方案
#1
2
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.
为了更清晰一点。App_Code文件夹在asp.net项目中有特殊的意义。您可以将文件放在那里,而asp.net将在运行时编译它们,并在发生变化时对它们进行监视,以便在动态的情况下重新编译。细节不是很相关,主要的是:如果不需要这个特性——不要使用App_Code文件夹。
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.
然而,这个文件夹本身并没有什么神奇之处。当您通过“添加ASP”创建它时。Visual Studio会设置你添加到内容中的文件的构建动作,甚至是代码文件。这样的文件不会被编译成结果程序集,这就是为什么您不能在单元测试项目中使用它们,甚至在web站点代码本身中也不能使用它们。如果您手动更改构建操作以编译——它将被编译并可以像往常一样使用。这就是区别。
#1
2
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.
为了更清晰一点。App_Code文件夹在asp.net项目中有特殊的意义。您可以将文件放在那里,而asp.net将在运行时编译它们,并在发生变化时对它们进行监视,以便在动态的情况下重新编译。细节不是很相关,主要的是:如果不需要这个特性——不要使用App_Code文件夹。
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.
然而,这个文件夹本身并没有什么神奇之处。当您通过“添加ASP”创建它时。Visual Studio会设置你添加到内容中的文件的构建动作,甚至是代码文件。这样的文件不会被编译成结果程序集,这就是为什么您不能在单元测试项目中使用它们,甚至在web站点代码本身中也不能使用它们。如果您手动更改构建操作以编译——它将被编译并可以像往常一样使用。这就是区别。