简体   繁体   English

单元测试ASP.Net Web应用程序的App_Code

[英]Unit Testing the App_Code of an ASP.Net Web Application

I want to create an ASP.Net Web Application, and I want to write unit tests for it. 我想创建一个ASP.Net Web应用程序,我想为它编写单元测试。 But my unit test project can't see any of the types in my web application's App_Code directory. 但我的单元测试项目无法在我的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. 打开Visual Studio。 Choose File->New->Project. 选择File-> New-> Project。
Choose Templates -> Visual C# -> Web. 选择模板 - > Visual C# - > Web。 Choose ASP.Net Web Application. 选择ASP.Net Web应用程序。 Choose "OK". 选择“确定”。

在此输入图像描述

Choose "Web Forms". 选择“Web窗体”。 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". 在解决方案资源管理器中,右键单击Web应用程序,然后选择“添加 - >添加ASP.Net文件夹 - > App_Code”。

在此输入图像描述 在此输入图像描述

Add a new item to App_Code, named "Util.cs". 将新项添加到App_Code,名为“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: 在单元测试项目中,将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?) 解决方案无法构建使用The type or namespace name 'App_Code' does not exist in the namespace 'WebApplication1' (are you missing an assembly reference?)

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. 我查看了存储在App_Code中的单元测试ASP.net网站项目代码 ,但我不认为问题完全符合我的情况,因为他们正在测试一个网站而我正在测试一个Web应用程序。 Some answers specifically suggest switching to a web application, implying that it would no longer be a problem: 一些答案特别建议切换到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 App项目 ,我认为它也更清洁

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. 答案还建议“尝试将代码移到自己的项目中”,我确实希望这样可行,但在我的实际解决方案中,我在App_Code目录中有几十个文件。 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. App_Code文件夹在asp.net项目中有特殊含义。 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. 您可以将文件放在那里,asp.net将在运行时编译它们,并监视它们的更改,以便在发生更改时动态重新编译。 Details are not very relevant, main thing is: if you don't need this feature - don't use App_Code folder. 细节不是很相关,主要的是:如果你不需要这个功能 - 不要使用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. 当您通过“添加ASP.NET文件夹”(或仅在网站项目中手动创建)创建它时 - Visual Studio将设置您添加到Content的文件的构建操作,即使对于代码文件也是如此。 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. 如果您手动将构建操作更改为Compile - 它将被编译并可以照常使用。 That's all the difference. 这就完全不同了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM