TDD:测试 MVC 应用程序

发布于 2024-12-05 04:32:39 字数 161 浏览 4 评论 0原文

我使用 TDD 和 PHPUnit 从头开始​​创建一个新应用程序。它将是一个 MVC 应用程序。我已经从模型开始了测试。这很有趣,而且我没有遇到太多问题。

现在我想创建我的视图/模型。但问题是:如何测试我的控制器?意见呢?我的控制器将使用我测试的模型和我的未来观点。

谢谢。

I've started from scratch a new application using TDD and PHPUnit. It is going to be a MVC application. I've started my tests from the Model. That was fun and i didn't have too much problems.

Now I want to create my views/models. But the question is: How do I test my controllers? What about views? My controller will use my tested models and my future views.

Thanks.

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

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

发布评论

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

评论(2

亽野灬性zι浪 2024-12-12 04:32:39

MVC 中的每一层都有三大类测试。模型的单元测试、控制器的功能测试和视图的 UI 测试。

单元测试是最容易编写的。它们在时间上很便宜,并且不需要太多的依赖项来进行存根/模拟。

另一方面,与单元测试相比,功能测试有点昂贵。在给定的场景中,如果您在应用程序中已经足够多地使用单元测试来覆盖模型,那么您可以在功能测试部分放松一点。但是,您仍然应该拥有良好的代码覆盖率 - 100%,这也是您的控制器方法的理想选择。

最后是 UI 测试,它是所有测试中成本最高的。您可能可以使用 Selenium 进行基于浏览器的测试,并尝试使用您的语言将其自动化。您可能需要在后台运行 RC 服务器才能实现此目的。但相信我,如果你涵盖了前两个部分——单元和功能,那么这可以是可选的,如果不是没有的话。

建议使用 CI - 持续集成设置和代码覆盖实用程序,该实用程序可以提供测试覆盖的代码百分比的趋势。

There are three broad categories of testing for each of the layer in the MVC. The unit tests for models, functional tests for the controllers and the UI testing for the views.

Unit tests are the easiest to write. They are cheap in terms of time and do not require too many dependencies to be stubbed/mocked.

Functional tests on the other hand are a bit expensive compared to an unit test. In a given scenario, if you had covered enough in your application for the models using unit test, you can relax a bit in the functional testing part. However, you should still be having a good code coverage - 100% to be ideal for your controller methods as well.

The last is the UI testing which is the costliest of all tests. You can probably use Selenium for browser based testing and try to automate it with your language. You may have to have a RC sever running in the background to achieve this. But trust me, if you cover the first two parts - unit and functional, this can be made optional if not no.

And it is advised to have a CI - Continious Integration setup with a code coverage utility that gives a trending on the % of code covered through tests.

自此以后,行同陌路 2024-12-12 04:32:39

当您运行测试时,您应该仅使用被测试的类。其他对象应该用模拟或其他假结构替换。

您这样做是因为(例如)当您为控制器操作编写测试时,您将为所述操作提供一些值,然后检查是否将正确的数据传递到视图和模型。

这是您应该避免在代码中使用全局状态(以静态调用或全局变量的形式)的原因之一。

您可能会发现一些有用的链接

When you are running the tests , you should be using only the class under test. Other objects should be replaced with mocks or other fake structures.

You do this, because ( for example ) when you write a test for controllers action , you will have provide some values for said action, and then check if the correct data is passed to the view and models.

This is one of the reasons why you should avoid use of global state ( in form of static calls or global variables ) in your code.

Some links you might find useful

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