简体   繁体   English

如何用Phpunit测试使用其他方法的抽象类中的方法

[英]How to Phpunit test a method in an abstract class that uses other methods

I have an abstract class as follows: 我有一个抽象类,如下所示:

abstract class Entity
{

  public function __construct($configFile, $logger, $database)
  {
    $this->configFile = $configFile;
    $this->logger = $logger;
    $this->database = $database;
  }


  public function SettingData(array $info)
  {
        $this->logger->debug('setting the information'.serialize($info));
        $info = $info.'debug';
        $this->formatData();
        return true;
    }
}

Here is the unittest: 这是单元测试:

  $data = array('informations','data');
  $result = Entity::SettingData($data);
  $this->assertTrue($result);

I want to test the method SettingData but I get the error: 我想测试方法SettingData,但出现错误:

Undefined property: Entity::$logger

How Am I supposed to test this method? 我应该如何测试这种方法?

Any help is really apreaciated. 真的很需要帮助。

You cannot test an abstract class. 您不能测试抽象类。 You have to create a derived class. 您必须创建一个派生类。 Then you create an object of that class and call the constructor with testable parameters. 然后,创建该类的对象,并使用可测试的参数调用构造函数。 After that you can test SettingData. 之后,您可以测试SettingData。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM