您对控制器进行了多少测试?

发布于 2024-08-05 08:35:13 字数 89 浏览 6 评论 0原文

我目前正在开始使用 BDD - 我之前没有编写过任何测试。 我总是尽量让我的模型保持胖而控制器瘦。

您认为控制器规格是否必要?

此致

I am currently beginning with BDD - I have not written any tests before.
I always try to keep my models fat and my controllers skinny.

What do you think - are controller specs necessary?

Best regards

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

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

发布评论

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

评论(3

淡墨 2024-08-12 08:35:14

是的。测试是否进行了正确的调用,以及是否在必要时进行了正确的重定向,并测试是否正在呈现正确的页面。因此,请测试您的应用程序是否按预期运行。

Yes. Test the correct calls are being made, and that the correct redirects and being made when necessary, and test that the correct pages are being rendered. So test that your application behaves as expected.

落日海湾 2024-08-12 08:35:14

就我自己而言,我试图理解在测试中寻求高水平的代码覆盖率与维护一组可能相当脆弱的测试的长期成本之间的平衡。

假设我们有一个控制器,它的

    try {
        result = mode.doSomething();

        if  (result.count == 0 )
             message = "none found"
             redisplay criterion page
        else if (result.count == 1 ) 
             display detail page
        else 
             display list page
    } catch exception {
        message = "bad things happened, please try again"
        redisplay criterion page
    }

初步想法是,三个计数情况(0、1、很多)的测试可能比例外情况的测试价值更低,并且更容易发生变化。价值较低,因为 a)。其他测试将发现页面显示中的问题 b)。测试非常接近于简单地复制代码。

    code: go to page X
    test: did you go to page X?

如果开发人员错误地选择了 X,他的测试也会出错!如果 UI 的可用性测试表明 Y 是更好的显示页面,那么我们会同时更新代码和测试。测试真的取得了什么成果吗?

而异常情况在正常的 UI 测试中可能很难执行,而使用模拟测试确实很容易。而且,我们确实需要纠正异常后的行为。

For myself I'm trying to understand the balance between seeking high levels of code coverage in testing and the long term cost of maintaing a set of tests that may be quite brittle.

Suppose we have a Controller that goes

    try {
        result = mode.doSomething();

        if  (result.count == 0 )
             message = "none found"
             redisplay criterion page
        else if (result.count == 1 ) 
             display detail page
        else 
             display list page
    } catch exception {
        message = "bad things happened, please try again"
        redisplay criterion page
    }

A tentative thought is is that tests of the three count cases (0, 1, many) may be less valuable, and more prone to change than the test of the exeption case. Less valuable because a). other testing will catch problems in page display b). the test gets very close to simply reproducing the code.

    code: go to page X
    test: did you go to page X?

If the developer makes the wrong choice of X he gets the test wrong too! If usability testing of the UI reveals that Y is better page to display then we update the code and the test in tandem. Did the test really achieve anything?

Whereas the exception case may be very hard to exercise in normal UI testing, and really easy to test with mocks. And, behaviour following exceptions is something that we really do need to get right.

尸血腥色 2024-08-12 08:35:14

您认为 - 控制器规格是否必要?

我认为如果将它们与良好的集成测试结合起来,它们就没有必要了。我发现应用程序的可用性非常重要,因此对于控制器/视图的每次更改,我都会单击应用程序并了解它的感觉。在我看来,为控制器和视图创建“哑”测试没有太多额外的价值。

对于集成测试,我使用 Cucumber。这使得测试整个堆栈成为可能,并确保您的应用程序按照您想要的方式执行。对于业务逻辑(胖模型),我仍然使用 RSpec。

What do you think - are controller specs necessary?

I don't think they are necessary if you combine them with a good integration test. I find the usability of the application very important, so for every change in controller / views, I click through the application and find out how it feels. Creating 'dumb' tests for controllers and views doesn't have much extra value in my opinion.

For integration testing I'm using Cucumber. This makes it possible to test the full stack and make sure your application performs in the way you want. For the business logic (the fat models) I still use RSpec.

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