用于文件上传的phpunit testcase

时间:2012-05-15 12:03:30

标签: php phpunit yii

任何人都可以帮助我。如何编写文件上传phpunit testcase? 我完成了插入,唯一数据插入,删除等功能。 以下是我的代码,但它无法正常工作

class FileuploadTest extends PHPUnit_Framework_TestCase
{

    public $testFile = array(
       'name'=>'2012-04-20 21.13.42.jpg',
       'tmp_name'=>'C:\wamp\tmp\php8D20.tmp',
       'type'=>'image/jpeg',
       'size'=>1472190,
       'error'=>0
    );


public function testFileupload()
    {   

        $testUpload = new Fileupload;
        $testUpload->image = new CUploadedFile($this->testFile['name'],$this->testFile['tmp_name'],$this->testFile['type'],$this->testFile['size'],$this->testFile['error']);
        $this->assertFalse($testUpload->validate());

        $errors= $testUpload->errors;
        $this->assertEmpty($errors);
    }

}

1 个答案:

答案 0 :(得分:1)

根据你的意见,这就是测试,$testUpload->validate()返回true,你试图assert如果它是假的,显然测试会失败。

如果$this->assertFalse($testUpload->validate());失败,则表示$testUpload已正确初始化,因此验证返回true。

要继续测试中的下一个断言,您需要使用

$this->assertTrue($testUpload->validate());

您需要了解有关单元测试的更多信息。网上有很多文章,简单的搜索将会返回。