单元测试ASP.Net Web应用程序的App_Code [英] Unit Testing the App_Code of an ASP.Net Web Application

查看:42
本文介绍了单元测试ASP.Net Web应用程序的App_Code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个ASP.Net Web应用程序,并且我想为其编写单元测试.但是我的单元测试项目在我的Web应用程序的App_Code目录中看不到任何类型.

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.

(如果您已经知道如何创建默认的Webforms Web应用程序并添加app_code文件夹,请跳过图像

(skip down past the images if you already know how to create a default webforms web application and add an app_code folder)

打开Visual Studio.选择文件"->新建"->项目".
选择模板-> Visual C#-> Web.选择"ASP.Net Web应用程序".选择确定".

Open Visual Studio. Choose File->New->Project.
Choose Templates -> Visual C# -> Web. Choose ASP.Net Web Application. Choose "OK".

选择"Web表单".选中添加单元测试".点击确定.

Choose "Web Forms". Check "Add unit tests". Click OK.

在解决方案资源管理器中,右键单击Web应用程序,然后选择添加->添加ASP.Net文件夹-> App_Code".

In the solution explorer, right-click on the web application and choose "Add -> Add ASP.Net Folder -> App_Code".

向App_Code添加一个名为"Util.cs"的新项.在文件中添加一个函数,使其内容为:

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;
        }
    }
}

在单元测试项目中,将UnitTest1.cs更改为:

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();
            }
        }
    }
}

然后构建项目.

应建立解决方案.如果我们尝试进行测试,则测试应该会成功.

The solution should build. If we attempt to test, the test should succeed.

解决方案无法使用生成.类型或名称空间名称'App_Code'在名称空间'WebApplication1'中不存在(您是否缺少程序集引用?)

我查看了单元测试ASP.net网站上存储在App_Code中的项目代码,但我认为问题完全符合我的情况,因为他们正在测试一个网站,而我正在测试一个Web应用程序.一些答案明确建议切换到Web应用程序,这意味着它不再是问题:

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:

但是,如果您可以选择将代码隔离到二进制文件(类库)中,或者仅使用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.

 

我将把这种逻辑移到其自己的类库项目中,或者按照Fredrik和Colin的建议将项目类型更改为Web应用程序.

 

并且正如OP所述,也可以移动到Web应用程序项目,我想这也更干净

尝试使用Web应用程序"建议不适用于我,因为我已经已经使用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.

答案还建议尝试将代码移至其自己的项目中",我确实希望这样做会奏效,但在我的实际解决方案中,App_Code目录中有许多文件.在开始建立测试框架之前,我不希望进行任何重大的结构更改.

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 文件夹在asp.net项目中具有特殊含义.您可以将文件放在此处,asp.net将在运行时对其进行编译,并将监视它们的更改以在发生某些更改时即时重新编译.详细信息不是很相关,主要是:如果您不需要此功能-请勿使用 App_Code 文件夹.

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.

但是,该文件夹本身没有什么神奇的东西.当您通过添加ASP.NET文件夹"创建该文件时(或只是在网站项目中手动创建),Visual Studio会设置您添加到 Content 的文件的构建动作,即使是代码文件也是如此.这样的文件不会被编译成最终的程序集,这就是为什么您不能在单元测试项目中使用它们,甚至从网站代码本身也不能使用它们的原因.如果您将构建操作手动更改为 Compile -将对其进行编译,并且可以照常使用.就是所有的不同.

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.Net Web应用程序的App_Code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆