简体   繁体   English

尝试在Drupal项目上运行PhpUnit测试时出现此错误

[英]I get this Error when trying to run a PhpUnit test on my Drupal project

TypeError: Argument 1 passed to Drupal\\views\\Plugin\\views\\HandlerBase::__construct() must be of the type array phpunit TypeError:传递给Drupal \\ views \\ Plugin \\ views \\ HandlerBase :: __ construct()的参数1必须是类型array phpunit

My code is: 我的代码是:

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

 }
}

I think the error is within " $test = new SimpleMathField(); because the test runs perfectly when I run it like this: 我认为错误在“$ test = new 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);

 }
}

The problem is not with your test, but how you instantiate the field. 问题不在于您的测试,而在于您如何实例化该字段。 Through a chain of abstraction the class extends HandlerBase and the constructor linked looks like this: 通过一个抽象链,该类扩展了HandlerBase ,链接的构造函数如下所示:

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

You could try something like this: 你可以尝试这样的事情:

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

You probably have to check in the plus() method if some of these variables passed to __construct or initialized by them is needed in there. 你可能必须检查plus()方法,如果其中一些传递给__construct或由它们初始化的变量。 You could also try something called partial mocking, where you disable the constructor but keep the tested method in test: 你也可以尝试一些叫做局部模拟的东西,你可以在其中禁用构造函数但是将测试的方法保留在测试中:

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

You might have to add some methods you need to be mocked in the array. 您可能必须添加一些需要在数组中进行模拟的方法。 Those will be replaced with whatever you specify using expects() and method() in your typical mocks. 这些将被您在典型模拟中使用expects()method()指定的任何内容替换。

Disclaimer: I'm not sure if you have to pass an empty array or an explicit null to setMethods to make this work as I rarely use partial mocking myself. 免责声明:我不确定您是否必须将空数组或显式空值传递给setMethods以使其工作,因为我自己很少使用部分模拟。 You have to check the docs or try yourself. 你必须检查文档或尝试自己。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试通过 phpunit.xml 测试我的 Laravel 模块时出现错误 - when i trying to test my Laravel module by phpunit.xml i get error 尝试使用PHPUnit运行Selenium测试时出错 - Error when trying to run a Selenium test with PHPUnit 当我运行 phpunit 测试时,Docker 似乎使用缓存 - Docker seems to use a cache when I run my phpunit test 为什么当我运行phpunit测试时,我得到一个空响应,但Postman中的相同路由/用户的行为符合预期? - Why when I run my phpunit test I get an empty response but the same route/user in Postman behaves as expected? Netbeans 8.2 中的 PHPUnit 插件给出了致命错误:尝试运行测试用例时找不到类“PHPUnit_Framework_TestSuite” - PHPUnit Plugin in Netbeans 8.2 gives Fatal error: Class 'PHPUnit_Framework_TestSuite' not found when trying to run a test case codeigniter错误:我试图运行我的项目,并从数据库中选择数据,但出现此错误: - codeigniter error: I am trying to run my project and Select data from the datatbase but I get this error: 尝试在Symfony 2.8中执行PHPUnit测试时出错 - Error when trying to execute PHPUnit Test in Symfony 2.8 当我尝试使用PHPUnit获得单元测试的代码覆盖率时,没有任何反应 - Nothing happens when i try to get code coverage for my Unit test with PHPUnit 我在尝试将Crinsane / LaravelShoppingcart包含到我的laravel项目中时遇到此错误: - i get this error when iam trying to include Crinsane/LaravelShoppingcart to my laravel project : Drupal - 为什么我不断收到错误消息“表变量已经存在”。 尝试在我的 XAMPP 本地主机上设置 drupal 时 - Drupal - Why do I keep getting error message "Table variable already exists." when trying to setup drupal on my XAMPP localhost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM