gpt4 book ai didi

asp.net-mvc - MVC 3 : Test controllers VS integration tests

转载 作者:行者123 更新时间:2023-12-05 09:00:00 24 4
gpt4 key购买 nike

我最近才开始使用 MVC,因为我听说 MVC 的主要优点是它使应用程序可以进行单元测试。在编写第一个单元测试后,我发现测试内部有很多逻辑的 Controller 并不总是那么简单(发送确认电子邮件、使用 session 、上下文和其他 ASP 网络静态)。编写单元测试比编写功能花费我更多的时间,我不相信这有用。

我很想将业务逻辑移动到一个“服务”层,该层消除了所有 ASP 网络静态信息并且可以轻松测试。然后使用 Selenium 进行集成测试,以测试整个功能。

  1. 当测试一个 Action 非常复杂(尤其是模拟输入和设置环境)时,您是否遇到过这种情况?

  2. 您找到在 Controller 中拥有业务逻辑的好方法了吗?或者您发现使用服务和 Controller 代码只是中继服务调用更好?

  3. 在我看来,测试 Controller 更像是集成测试,而不是单元测试。你怎么看这件事?

  4. 您认为单元测试 Controller 比集成测试有什么优势吗?

最佳答案

I am tempted to move the business logic into a "Service" layer that eliminates of all ASP Net statics and which can be easily tested. Then to use Selenium for integration tests in order to test the whole functionality.

这几乎就在这里。如果您的 Controller 很复杂,则需要重构它们。他们根本不应该有任何业务逻辑。您可以使用 Mock 框架来模拟服务层并以这种方式轻松测试您的 Controller 。

In my opinion testing a controller is more equivalent to integration tests than to unit tests. What do you think about this?

我不同意这一点。您正在测试您的 Controller 以确保它根据您提供的输入返回适当的响应。提供一个不存在的ID?重定向到另一个页面或返回 NotFound View 。模型状态无效?再次返回相同的 View 等。

关于asp.net-mvc - MVC 3 : Test controllers VS integration tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10281692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com