2017-04-20 58 views
1

类型错误运行PHPUnit的测试时,出现此错误:传递给Drupal的\意见\插件\观点参数1 \的HandlerBase :: __结构()必须是数组类型的PHPUnit我想对我的Drupal项目

我代码:

use Drupal\views_simple_math_field\Plugin\views\field\SimpleMathField; 

class BasicTest extends PHPUnit_Framework_TestCase 
{ 
    public function test_proba() 
    { 
    $first = 25; 
    $second = 5; 
    $result = 13; 
    $test = new SimpleMathField(); 
    $working = $test->plus($first,$second,$result); 
    $this->assertEquals($result,$working); 

} 
} 

我认为错误是“$测试=新SimpleMathField()内;由于测试运行完美,当我运行它是这样的:

<?php 

use Drupal\views_simple_math_field\Plugin\views\field\SimpleMathField; 

class BasicTest extends PHPUnit_Framework_TestCase 
{ 
    public function test_proba() 
    { 
    $first = 25; 
    $second = 5; 
    $result = 13; 
    $this->assertTrue(True); 

} 
} 
+0

你测试一个名为Basic类? –

+0

我测试在SimpleMathFIeld班加()函数,它位于有Drupal的\ views_simple_math_field \插件\意见\现场\命名空间中的extern PHP文件。 –

+0

测试类应该有一个带有“Test”后缀的名称。所以它应该是SimpleMathFIeldTest而不是BasicTest。如果你不是很确定自己在做什么,我会推荐这个优秀的教程:https://jtreminio.com/2013/03/unit-testing-tutorial-introduction-to-phpunit/ –

回答

0

的问题不在于你测试,但你是如何实例化该字段。通过抽象的连锁类扩展HandlerBase和构造链接看起来像这样:

public function __construct(array $configuration, $plugin_id, $plugin_definition) { 
    parent::__construct($configuration, $plugin_id, $plugin_definition); 
    $this->is_handler = TRUE; 
} 

你可以尝试这样的事:

new SimpleMathField(array(), 'test_id', 'test_definition'); 

你可能有在plus()方法,如果某些检查这些变量传递给__construct或由它们初始化是需要的。您也可以尝试一种叫做局部的嘲弄,在那里你禁用的构造,但保持测试方法测试:

$partiallyMockedField = $this->getMockBuilder(SimpleMathField::class) 
    ->disableOriginalConstructor() 
    ->setMethods([]) 
    ->getMock(); 

您可能需要添加一些你需要在阵列中被嘲笑的方法。这些将被替换为您在典型模拟中使用expects()method()指定的内容。

声明:我不确定您是否必须传递一个空数组或明确的null来setMethods来使这项工作,因为我很少使用部分嘲笑自己。你必须检查文档或尝试自己。

+0

非常感激 我可以”尽管如此,声誉仍然很低。 –

相关问题