Unit testing 联合测试:序列化';Symfony\Component\HttpFoundation\File\UploadedFile';不允许

Unit testing 联合测试:序列化';Symfony\Component\HttpFoundation\File\UploadedFile';不允许,unit-testing,symfony,Unit Testing,Symfony,我正在尝试编写white测试,用文件上传来测试我的API 我正在使用基本的客户端请求,而不是爬虫 单元测试是: class RecordsControllerTest extends WebTestCase { private $client; public function __construct() { parent::__construct(); $this->client = self::createClient(); $this->client-

我正在尝试编写white测试,用文件上传来测试我的API

我正在使用基本的客户端请求,而不是爬虫

单元测试是:

class RecordsControllerTest extends WebTestCase {

private $client;

public function __construct() {
    parent::__construct();
    $this->client = self::createClient();
    $this->client->insulate();
}

public function testApiPostUpload($params){
    $fileToUpload = realpath(__DIR__.'/../../resources/mpthreetest.mp3');
    $file = new UploadedFile(
        $fileToUpload,
        'mpthreetest.mp3',
        MimeTypeGuesser::getInstance()->guess($fileToUpload),
        filesize($fileToUpload)
    );
    $this->client->request('POST', '/records/'.$params['createdRecordId'].'/upload', array(), array('file' => $file) );

    $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
}
}
执行测试时,我收到一个错误:

Exception: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed

/path/to/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:165
/path/to/project/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:348
/path/to/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:143
/path/to/project/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:313
/path/to/project/src/Bundle/Tests/Functional/Controller/RecordsControllerTest.php:182
我发现了大约相同的错误,但在这种情况下,请求没有发送到控制器,问题不在于实体和实现序列化

有人知道如何解决这个问题吗


任何试图在symfony 2中对上载文件进行单元测试的人?

您可以尝试不隔离将false作为参数传递给隔离方法的请求,因此请尝试以下操作:

$this->client->insulate(false);
与此相反:

$this->client->insulate();

希望此帮助

我能够通过将
changeHistory
参数设置为false(请求方法签名中的第7个也是最后一个参数)来解决此问题:

这将阻止在以下行上序列化:

if ($this->followRedirects && $this->redirect) {
        $this->redirects[serialize($this->history->current())] = true;
        return $this->crawler = $this->followRedirect();
}

这解决了问题。谢谢我也有同样的问题,但对我来说,
insulate(false)
并不能解决问题。
if ($this->followRedirects && $this->redirect) {
        $this->redirects[serialize($this->history->current())] = true;
        return $this->crawler = $this->followRedirect();
}