问题描述
我有一个 ASP.net 网站项目 (.net 3.5).目前所有非代码隐藏的代码文件(包括 Linq2Sql 的东西、数据上下文、业务逻辑、扩展方法等)都位于 App_Code 文件夹中.
I have an ASP.net Web Site Project (.net 3.5). Currently all of the non-code behind code files (including Linq2Sql stuff, data contexts, business logic, extension methods, etc) are located in the App_Code folder.
我有兴趣至少在项目的某些部分引入单元测试(使用 nunit).我将要进行的任何单元测试都需要拥有对当前位于 App_Code 文件夹中的所有代码的完全访问权限.到目前为止,我已经做了一些初步阅读,共识似乎是:
I am interested in introducing Unit Testing (using nunit) in at least some sections of the project moving forward. Any Unit Testing that I would be doing would need to have full access to all of the code that is currently located in the App_Code folder. I have done some initial reading so far, and the consensus seems to be:
- 鉴于我目前的设置,这是不可能的
- 单元测试需要引用属于已编译 dll 的类,而根据定义,网站项目仅在运行时编译.
- 为了继续,我需要将我的整个项目转换为 Web 应用程序,或者将我想要测试的所有代码(即:App_Code 的全部内容)移动到类库项目并引用网站项目中的类库项目.其中任何一个都可以访问我需要的已编译 dll 格式的类,这将允许我对它们进行单元测试.
这是正确的吗?或者有没有另一种方法可以让我在不重组/重构整个项目的情况下进行单元测试?
Is this correct? Or is there another way that I can Unit Test without restructuring/refactoring my entire project?
推荐答案
您的结论似乎是正确的.我会投票支持将功能转移到一个或多个类库项目中,因为这也可能为在其他项目中重用相同的功能打开大门.
Your conclusions seem correct. I would vote for moving functionality into one or several class library projects, since that may open the door for reusing the same functionality in other projects as well.
这篇关于单元测试存储在 App_Code 中的 ASP.net 网站项目代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!