Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 66659

Use it for questions about automated Drupal tests.

0 votes

PHPUnit calling external function as object

You probably need to change this: use cvr\src\Cvrapi\CVRClient; To this: use Cvrapi\CVRClient; And change this: $result = \Cvrapi\CVRClient::request('29910251', 'dk', 'vat', 'Unit Testing'); To … this: $result = CVRClient::request('29910251', 'dk', 'vat', 'Unit Testing'); …
Jaypan's user avatar
  • 18k
3 votes

How do I dump variables to the screen when running PHPUnit tests?

Well, I found a solution that works. I'm not sure if it is the best way to do it, but actually, it its own way it seems like it is probably the way it's supposed to be done. First, I discovered that …
Jaypan's user avatar
  • 18k
3 votes

Call to undefined function t() in an Unit test

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->t() Then you can use the …
Jaypan's user avatar
  • 18k
0 votes

Basic kernel test fails with Type of Drupal\KernelTests\Core\Entity\EntityKernelTestBase::$m...

From KernelTestBase: /** * Modules to enable. * * The test runner will merge the $modules lists from this class, the class * it extends, and so on up the class hierarchy. It is not necessary …
Jaypan's user avatar
  • 18k
2 votes

Running tests for a module without requiring a full site installation

With unit testing, you don't declare dependencies upon foreign code. This is because tests unit test a piece of code, not the foreign code. …
Jaypan's user avatar
  • 18k
0 votes
Accepted

Tests for a custom class that load entities

If you are doing Unit tests (and not functional tests), you will need to mock the objects you want to use, in the setup of the test. You can read more here: https://www.drupal.org/docs/8/phpunit/mocki …
Jaypan's user avatar
  • 18k
13 votes

How to create a unit test for a class which uses a trait?

Following on Bedir's answer, here is an example. I have a test that is using the class DurationService. DurationService uses the StringTranslationTrait as follows: class DurationService implements Dur …
Jaypan's user avatar
  • 18k
13 votes
Accepted

"\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a r...

You need to mock the services you will be using in your test, so that they return values. You can do that as follows: public function testDefaultBehavior() { $some_resolver = $this->getMockBuilder( …
Jaypan's user avatar
  • 18k