I want to write a Unit test. But the class I want to test has a AccountInterface dependencie Injection.
When I try to get the AccountInterface Object I get:
Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.
My test looks like this
<?php
namespace Drupal\mymodule\test;
use Drupal\mymodule\MyModuleQuery;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\User;
class MyModuleQueryTest extends UnitTestCase {
protected $user;
public function __construct() {
$this->user = \Drupal::currentUser();
}
public function testResult() {
$MyModuleQuery = new MyModuleQuery(1, $this->user);
$countUser = $MyModuleQuery->mymodule_get_user();
$countNode = $MyModuleQuery->mymodule_get_node();
$bool = FALSE;
if (!isset($countNode) && !isset($countUser)) {
$bool = TRUE;
}
$this->assertTrue($bool);
}
}