PHPUnit: attempt to use @cover or @use not existing method

I am learning unit test with PHPUnit 4.3.5 / PHP 5.5.14. Everything went fine until I tried to get the code. I get this error: "Trying @cover or @use of the non existing method" MyClass :: __ construct "while trying to get code coverage. I tried other SO answers but couldn't fix it. Any ideas what I'm doing wrong Thanks!

/** * Test constructor. * @covers MyClass::__construct * @group MyClassTest */ public function test_Can_Create_MyClass_Instance() { $this->assertInstanceOf('MyClass', $this->_myClass); } 
+8
php unit-testing phpunit
source share
2 answers

If your class implements the __construct method, then the problem is that the class itself was not found. Start removing the @covers annotation and see if the class can load. For example, try: var_dump(class_exists('MyClass')); inside the test (until the statement, which I believe will fail).

In annotations, and generally when passing the class name as a string, you should always refer to the class using the fully qualified name with the names:

 \MyClass \MyNamespace\MyClass 
+7
source share

You may get the same error if you expect the @covers annotation to work with a use statement with a namespace.

The following code will not work:

 <?php namespace MyCompany\MyBundle\Test\UnitTest\Service; use MyCompany\MyBundle\Service\ClassToTest; class MyAwesomeTest { /** * @covers ClassToTest::someMethod() */ public function testSomeMethod() { // do your magic } } 

Intelligent IDEs such as PHPStorm resolve the annotation of ClassToTest::someMethod() if you press CTRL +, but PHPUnit will give you the error Trying to @cover or @use not existing method "ClassToTest::someMethod". .

Here's a pull request here: https://github.com/sebastianbergmann/phpunit/pull/1312

For work, just use the fully qualified class name:

 <?php namespace MyCompany\MyBundle\Test\UnitTest\Service; use MyCompany\MyBundle\Service\ClassToTest; class MyAwesomeTest { /** * @covers \MyCompany\MyBundle\Service\ClassToTest::someMethod() */ public function testSomeMethod() { // do your magic } } 
+12
source share

All Articles