2015-01-31 56 views
0

通过作曲家更新我的代码之前,我的表单单元测试正在工作。现在在延期模拟上失败了。 继承人的错误:Symfony单元测试表单扩展模拟

Argument 1 passed to Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension::__construct() must be an instance of Symfony\Component\Validator\ValidatorInterface, instance of Mock_ValidatorInterface_14b95ba0 given 

我的继承人作曲家需要部分:

"require": { 
    "php": ">=5.3.3", 
    "symfony/symfony": "~2.3", 
    "doctrine/orm": "~2.2,>=2.2.3", 
    "doctrine/doctrine-bundle": "~1.2", 
    "twig/extensions": "~1.0", 
    "symfony/assetic-bundle": "~2.3", 
    "symfony/swiftmailer-bundle": "~2.3", 
    "symfony/monolog-bundle": "~2.3", 
    "symfony/monolog-bridge": "~2.4", 
    "sensio/distribution-bundle": "~2.3", 
    "sensio/framework-extra-bundle": "~2.3" 
    } 

继承人在单元测试设置方法验证模拟:

use Symfony\Component\Form\Test\TypeTestCase; 
use Symfony\Component\Form\Forms; 
use Symfony\Component\Form\FormBuilder; 
use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension; 
use Symfony\Component\Validator\ConstraintViolationList; 
use Symfony\Component\Form\Extension\Core\CoreExtension; 

class TestedTypeTest extends TypeTestCase 
{ 

protected function setUp() 
{ 
    parent::setUp(); 

    $validator = $this->getMock('\Symfony\Component\Validator\Validator\ValidatorInterface'); 
    $validator->method('validate')->will($this->returnValue(new ConstraintViolationList())); 
    $formTypeExtension = new FormTypeValidatorExtension($validator); 
    $coreExtension = new CoreExtension(); 

    $this->factory = Forms::createFormFactoryBuilder() 
      ->addExtensions($this->getExtensions()) 
      ->addExtension($coreExtension) 
      ->addTypeExtension($formTypeExtension) 
      ->getFormFactory(); 
} 

有谁知道什么可能是错误的与模拟?任何意见是极大的赞赏。

+1

必须是\ Symfony \ Component \ Validator \ ValidatorInterface“vs”\ Symfony \ Component \ Validator \ Validator \ ValidatorInterface“的实例似乎在模拟中有太多的'验证器'。 – 2015-01-31 23:54:48

+0

[Symfony 2.5中的ValidatorConstraint问题]的可能重复(http://stackoverflow.com/questions/24009719/problems-with-validatorconstraint-in-symfony-2-5) – Brejk 2015-01-31 23:58:01

+0

@AlisterBulman是的,这就是它。删除第二个验证工作。我使用Symphony〜2.3而不是2.5,所以Symfony \ Component \ Validator \ Validator的弃用在这里不适用。 – pfwd 2015-02-01 08:01:48

回答

0

你必须逃离反斜杠在字符串中使用它们:

$validator = $this->getMock('\\Symfony\\Component\\Validator\\Validator\\ValidatorInterface');

2

从UPGRADE-2.5.md:

The validation engine in Symfony\Component\Validator\Validator was replaced by a new one in Symfony\Component\Validator\Validator\RecursiveValidator . With that change, several classes were deprecated that will be removed in Symfony 3.0. Also, the API of the validator was slightly changed. More details about that can be found in UPGRADE-3.0.

这里是类似的问题: Problems with ValidatorConstraint in Symfony 2.5