用于文件上传的 phpunit 测试用例


phpunit testcase for file upload

有人能帮我吗?如何编写文件上传phpunit测试用例?我这样做是为了插入,独特的数据插入,删除等功能。以下是我的代码,但它无法正常工作

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);
    }
}

根据您的评论,这就是测试,$testUpload->validate()返回 true,您正在尝试assert如果它是假的,显然测试会失败。

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

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

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

您需要阅读有关单元测试的更多信息。网络上有很多文章,只需搜索一下就会返回。