Call to undefined function t() in an Unit test I'm trying to test a method from the dev version of the Allow a content type only once (Only One) module that have a t() call inside:

&lt;?php

namespace Drupal\onlyone\Utility;

class InternalFunctions {


  public static function getLanguageLabel($language) {
    switch ($language) {
      case 'und':
      case '':
        // If the language is empty then is Undetermined.
        return t('Not specified');

      case 'zxx':
        return t('Not applicable');

      default:
        return ucfirst($language);
    }
  }

}


With the following Unit test:

&lt;?php

namespace Drupal\Tests\onlyone\Unit;

use Drupal\onlyone\Utility\InternalFunctions;
use Drupal\Tests\UnitTestCase;


class InternalFunctionsTest extends UnitTestCase {


  public function testGetLanguageLabel($language_code, $expected) {
    $this-&gt;assertEquals($expected, InternalFunctions::getLanguageLabel($language_code));
  }

  public function providerGetLanguageLabel() {
    $tests[] = ['', 'Not specified'];
    $tests[] = ['und', 'Not specified'];
    $tests[] = ['zxx', 'Not applicable'];
    $tests[] = ['es', 'Es'];
    $tests[] = ['en', 'En'];

    return $tests;
  }
}


and when I run the test I receive these errors:

1) Drupal\Tests\onlyone\Unit\InternalFunctionsTest::testGetLanguageLabel with data set #0 ('', 'Not specified')
Error: Call to undefined function Drupal\onlyone\Utility\t()

/Users/adriancid/Sites/drupal84-test/modules/drupal.org/onlyone/src/Utility/InternalFunctions.php:26
/Users/adriancid/Sites/drupal84-test/modules/drupal.org/onlyone/tests/src/Unit/InternalFunctionsTest.php:30

2) Drupal\Tests\onlyone\Unit\InternalFunctionsTest::testGetLanguageLabel with data set #1 ('und', 'Not specified')
Error: Call to undefined function Drupal\onlyone\Utility\t()

/Users/adriancid/Sites/drupal84-test/modules/drupal.org/onlyone/src/Utility/InternalFunctions.php:26
/Users/adriancid/Sites/drupal84-test/modules/drupal.org/onlyone/tests/src/Unit/InternalFunctionsTest.php:30

3) Drupal\Tests\onlyone\Unit\InternalFunctionsTest::testGetLanguageLabel with data set #2 ('zxx', 'Not applicable')
Error: Call to undefined function Drupal\onlyone\Utility\t()


But the t() function is always defined inside Drupal. If I remove the t() calls inside the method the test works fine.

I found in Unit testing more complicated Drupal classes that:


  ... global functions do not exist in the testing environment.


And a method to made this works, but I find that this is complicate things just to use a test and method with the t() function inside.

So, there is not a simple method to test a class method that have a t() call inside?
You should not be using t() inside classes. The string translation trait should be included instead (if it is not already part of a parent class). Then you will use:

$this-&gt;t()


Then you can use the answer I provided here as to how to do unit tests with string translation trait: How to unit test a class which uses a trait?