在 MVC 上应用 TDD

发布于 2024-10-20 22:33:50 字数 118 浏览 1 评论 0原文

我正在努力寻找 MVC 和 TDD 的好内容。我只是想学习如何有效地为 MVC 设计模式编写单元测试。我将不胜感激任何有用资源的见解或链接。 MVC 的哪些部分最适合单元测试?对视图进行单元测试有什么好处,如果有的话如何?

I am struggling to find good content of MVC and TDD. I just want to learn how to effectively write unit tests for the MVC design pattern. I would appreciate any insights or links to useful resources. Which parts of the MVC are ideal for unit testing? Any benefits on unit testing the view, and if so how?

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

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

发布评论

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

评论(2

傲性难收 2024-10-27 22:33:50

最好将单元测试应用于所有三个 - 模型、视图和控制器。这对于模型和控制器来说很简单,但对于视图部分来说可能很困难。 MVP 模式使这变得更容易,因为 MVP 中的视图通常比 MVC 中的视图更薄。

为了补充单元测试,您需要一些集成测试 - 这是一个广泛的主题,涵盖从测试少数对象的交互到测试完整系统的所有内容。自动化验收测试也很有帮助。

至于是否对任何给定的 M、V 或 C 进行单元测试:这归结为实现所需交付的质量水平所涉及的经济因素。通常,从长远来看(考虑回归测试)对每个部件进行单元测试比手动重新测试所有组件更便宜。事实上,便宜得多。想想手动测试和重新测试数百个类所涉及的成本,而您可以一路编写单元测试。

特别是对于视图,仍然考虑经济性和质量,如果该层非常薄,Selenium 或类似的工具可能可以为您提供测试视图所需的自动化。

It's best to apply the unit tests to all three - the model, the view, and the controller. This is straightforward for the model and the controller, but can be difficult for the view portion. The MVP pattern makes this somewhat easier, since the view in MVP is typically thinner than in MVC.

To complement the unit tests, you'll want some integration testing - a broad topic, covering everything from testing the interaction of a handful of objects to testing a complete system. Automated acceptance tests can be helpful, too.

As far as whether to unit test any given of the M, V, or C: It boils down to the economics involved in achieving the level of quality you need to deliver. Typically, it's cheaper in the long run (think regression testing) to have each part unit tested than it is to manually re-test all the components. In fact, far and away cheaper. Think of the cost involved in manually testing re-testing hundreds of classes, when you could have written units tests along the way.

Specifically for the view, still thinking economics and quality, if that layer is very thin, it might be that Selenium or similar tools can give you the automation you want for testing the view.

寂寞笑我太脆弱 2024-10-27 22:33:50

Ruby on Rails 内置了 MVC 和单元测试(我知道您的问题是关于 Java 的,但从高层次来看,方法应该大致相同)。

单元测试几乎专门用于测试模型(MVC 中的 M)。

然后是测试控制器(C)的功能测试。

如果您想测试 V,那么 Selenium 等工具是一个非常好的选择。

这是一篇关于 Rails 测试的好文章: http://guides.rubyonrails.org/testing.html< /a>

Ruby on Rails has both MVC and unit testing baked in (I know your question is about Java, but at a high level the approach should be largely the same).

Unit tests are pretty much exclusively to do with testing the Model (The M in MVC).

Then there are functional tests which test the Controller (the C).

If you want to test the V, then a tool such as Selenium is a very good bet.

This is a good article about testing in Rails: http://guides.rubyonrails.org/testing.html

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