【问题标题】:What assertions/tests are needed in a basic "Save/Submit" model test case?基本的“保存/提交”模型测试用例需要哪些断言/测试?
【发布时间】:2011-10-12 12:38:14
【问题描述】:

谁能帮助我在 CakePHP 2.0 的基本保存方法测试用例中需要哪些断言?

我有 Product、User 和 News 模型,我希望在 News 模型中为 submit 方法编写一个测试用例,并且有很多方法/东西要包括我只是想知道实际需要什么什么不是。我为所有型号设置了基本的固定装置。

我正在测试的方法实际上是这样的:

class News extends AppModel {
    public submit($productId, $userId, $newsData) { 
        // Logic which checks for user and products existence, saves and returns submitted row
    }
}

测试用例

public function testSubmit() {

    // Save News
    $newsData = array(
        'News' => array(
            'title' => 'Here is the title of the news',
            'body' => 'Here is the news body',
            'source' => 'News came from here'
        )
    );

    $news = $this->News->submit('product-1', 'user-1', $newsData);

    // Now what?
}

【问题讨论】:

    标签: unit-testing cakephp assertions cakephp-2.0


    【解决方案1】:

    只需断言 $news 是一个数组,对象,该数组等于您期望的数组...无论您的方法返回什么,您甚至在实现该方法(测试驱动开发)之前就应该知道它并且能够使用一种或多种 phpunit 断言方法断言结果。

    就像简单的 $this->assertTrue($news);检查所有断言的手册。 http://www.phpunit.de/manual/current/en/

    还可以查看 CakePHP 核心测试以了解如何进行测试。

    或查看一些开源插件示例,例如 https://github.com/CakeDC/tags/blob/2.0/Test/Case/Model/TaggedTest.php 或者 https://github.com/CakeDC/users/blob/2.0/Test/Case/Model/UserTest.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-27
      • 2014-08-06
      • 2013-03-13
      • 2013-12-30
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      相关资源
      最近更新 更多