如何使用 ASP.NET MVC 和单元测试组织代码和测试

发布于 2024-10-27 04:41:53 字数 318 浏览 1 评论 0原文

因此,我硬着头皮尝试开始使用 asp.net MVC、单元测试和 TDD。

我对所涉及的概念有一个模糊的理解,也就是说有点超出了“Hello World”的水平,但仍然很新。我已经准备好迎接路面了,但是 我发现自己在过去的半个小时里一直盯着 VS 中的“新项目”对话框...您到底是如何组织单元测试的?

我通过标准 VS 单元测试看到了这一点项目类型,它为单元测试创​​建一个单独的项目。使用 NUnit 时我应该这样做吗?或者应该将测试放置在与正在测试的代码相同的项目中?

我发现的“单元测试入门...”类型的教程似乎都没有解决这个问题。

So, I'm biting the bullet and trying to get started with asp.net MVC, unit testing and TDD.

I have a vague understanding of the concepts involved, which is to say somewhat beyond the "Hello World" level, but still pretty green. I'm ready for the rubber to meet the road, but
I've found myself staring at the "New Project" dialog in VS for the last half hour... how, exactly, do you organize your unit tests?

I see that with the standard VS Unit Test project type, it creates a separate project for the unit tests. Is that how I should proceed when using NUnit? Or should the tests be placed in the same project as the code being tested?

None of the "getting started with unit testing..." type tutorials I've found seem to address this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软甜啾 2024-11-03 04:41:53

测试应该在单独的项目中维护,因为您不想将测试代码部署到生产环境。对于单一项目解决方案,一个测试项目可能就足够了。

在内部,测试项目可以按照您认为方便的任何方式进行组织,只要保持一致即可。 ASP.NET MVC 测试项目可能有一个 ControllerTests 文件夹,每个控制器有一个测试 .cs 文件,在某种程度上反映了 MVC 项目结构。这使得测试很容易找到并与他们正在测试的代码相关联。

Tests should be maintained in separate projects because you don't want to deploy testing code to a production environment. For a single-project solution, one test project is probably sufficient.

Internally, a test project can be organized in any way you find convenient, as long as it's consistent. An ASP.NET MVC test project might have a ControllerTests folder with one test .cs file per controller, mirroring the MVC project structure to some degree. This makes the tests easy to find and relate to the code they're testing.

屋檐 2024-11-03 04:41:53

您到底是如何组织单元测试的?

我组织单元测试来反映我的项目结构。例如,如果在我的 ASP.NET MVC 项目中我有

  • 控制器、
  • 模型、
  • 映射器、
  • 验证器
    ...

我的单元测试项目中有相同的文件夹。然后对于 MVC 项目中的每个文件我都有一个相应的单元测试。以下是我编写的示例项目结构的示例。

how, exactly, do you organize your unit tests?

I organize my unit tests to reflect my project structure. So fir example if in my ASP.NET MVC project I have

  • Controllers
  • Models
  • Mappers
  • Validators
    ...

I have those same folders in my unit test project. Then for each file in the MVC project I have a corresponding unit test. Here's an example of a sample project structure I wrote.

分分钟 2024-11-03 04:41:53

我的格式通常是这样的:

MyMvcApp.Web (The Actual Web Application)
  |- Controllers
  |- ViewModels
  |- Views
  |- Framework (For specific override points in the MVC Framework)
MyMvcApp (The class library that contains my domain specific logic)
  |- SomeFacet (Folder to contain entities, objects, etc)
   |- Repositories
MyMvcApp.UnitTests (Test project)
  |- SomeFacet (Contains tests for specified folder in class library)
MyMvcApp.IntegrationTests (Test project)
  |- SomeFacet (Contains tests for specified folder in class library)

My format has usually been like so:

MyMvcApp.Web (The Actual Web Application)
  |- Controllers
  |- ViewModels
  |- Views
  |- Framework (For specific override points in the MVC Framework)
MyMvcApp (The class library that contains my domain specific logic)
  |- SomeFacet (Folder to contain entities, objects, etc)
   |- Repositories
MyMvcApp.UnitTests (Test project)
  |- SomeFacet (Contains tests for specified folder in class library)
MyMvcApp.IntegrationTests (Test project)
  |- SomeFacet (Contains tests for specified folder in class library)
初相遇 2024-11-03 04:41:53

更重要的是首先要了解我们需要测试什么以及有哪些设施可用于此目的。它基本上是您需要关注的应用程序中对象的行为。您有很多测试框架可供选择,例如 NUnit 等。继续构建测试项目。

It is more important to understand first that what do we need to test and what facilities are available for the purpose. It is basically the behaviour of the objects in the application you need to focus on. You have plenty of testing frameworks to choose from like NUnit Etc. Work on the structure the Test project as you carry on along with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文