0

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

我有产品、用户和新闻模型,我正在为模型中的submit方法编写一个测试用例,News并且有很多方法/东西要包括我只是想知道什么是真正需要的,什么不是。我有所有型号的基本夹具设置。

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

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?
}
4

1 回答 1

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.phphttps://github.com/CakeDC/users/blob/2.0 /Test/Case/Model/UserTest.php

于 2011-10-12T15:34:11.867 回答