运行phpunit时出现如下错误

时间:2014-04-17 10:05:58

标签: phpunit

请帮我解决这个问题

Configuration read from /usr/share/nginx/www/laravel-cribb/phpunit.xml

PHP Fatal error:  Class User contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Auth\UserInterface::getRememberToken, Illuminate\Auth\UserInterface::setRememberToken, Illuminate\Auth\UserInterface::getRememberTokenName) in /usr/share/nginx/www/laravel-cribb/app/models/User.php on line 93
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:583
PHP   3. PHPUnit_TextUI_Command->run() phar:///usr/bin/phpunit/phpunit/TextUI/Command.php:132
PHP   4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/bin/phpunit/phpunit/TextUI/Command.php:179
PHP   5. PHPUnit_Framework_TestSuite->run() /usr/share/nginx/www/laravel-cribb/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:349
PHP   6. PHPUnit_Framework_TestSuite->run() /usr/share/nginx/www/laravel-cribb/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:705
PHP   7. PHPUnit_Framework_TestSuite->runTest() /usr/share/nginx/www/laravel-cribb/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745
PHP   8. PHPUnit_Framework_TestCase->run() /usr/share/nginx/www/laravel-cribb/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775
PHP   9. PHPUnit_Framework_TestResult->run() /usr/share/nginx/www/laravel-cribb/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:783
PHP  10. PHPUnit_Framework_TestCase->runBare() /usr/share/nginx/www/laravel-cribb/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:648
PHP  11. UserRepositoryTest->setUp() /usr/share/nginx/www/laravel-cribb/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:835
PHP  12. Illuminate\Support\Facades\App::make() /usr/share/nginx/www/laravel-cribb/app/tests/unit/Repositories/UserRepositoryTest.php:12
PHP  13. Illuminate\Support\Facades\Facade::__callStatic() /usr/share/nginx/www/laravel-cribb/app/tests/unit/Repositories/UserRepositoryTest.php:12
PHP  14. Illuminate\Foundation\Application->make() /usr/share/nginx/www/laravel-cribb/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:208
PHP  15. Illuminate\Container\Container->make() /usr/share/nginx/www/laravel-cribb/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:463
PHP  16. Illuminate\Container\Container->build() /usr/share/nginx/www/laravel-cribb/vendor/laravel/framework/src/Illuminate/Container/Container.php:425
PHP  17. Cribbb\Repositories\RepositoryServiceProvider->Cribbb\Repositories\{closure}() /usr/share/nginx/www/laravel-cribb/vendor/laravel/framework/src/Illuminate/Container/Container.php:482
PHP  18. spl_autoload_call() /usr/share/nginx/www/laravel-cribb/vendor/laravel/framework/src/Illuminate/Container/Container.php:32
PHP  19. Composer\Autoload\ClassLoader->loadClass() /usr/share/nginx/www/laravel-cribb/vendor/laravel/framework/src/Illuminate/Container/Container.php:0
PHP  20. Composer\Autoload\includeFile() /usr/share/nginx/www/laravel-cribb/vendor/composer/ClassLoader.php:269
PHP  21. include() /usr/share/nginx/www/laravel-cribb/vendor/composer/ClassLoader.php:377

2 个答案:

答案 0 :(得分:1)

您尚未展示您的测试,但您正在尝试使用抽象类,并且没有实现所有抽象方法。正如错误所述。

您可能正在尝试模拟抛出错误并仅使用getMock()的类。您需要使用getMockForAbstractClass()代替。这将负责为您实施这些方法。

$mock = $this->getMockForAbstractClass('User');

or using the MockBuilder

$mock = $this->getMockBuilder('User')
             ->getMockForAbstractClass();

http://phpunit.de/manual/current/en/test-doubles.html#test-doubles.mocking-traits-and-abstract-classes

答案 1 :(得分:0)

错误消息已指出正确的方向:

PHP Fatal error:  Class User contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Auth\UserInterface::getRememberToken, Illuminate\Auth\UserInterface::setRememberToken, Illuminate\Auth\UserInterface::getRememberTokenName) in /usr/share/nginx/www/laravel-cribb/app/models/User.php on line 93

我假设您的代码如下所示:

class User {
   getRememberToken();
   setRememberToken($rememberToken);
   getRememberTokenName();
}

您需要做的事情现在取决于您想要做什么。

如果用户应该是抽象类:

abstract class User {
   getRememberToken();
   setRememberToken($rememberToken);
   getRememberTokenName();
}

如果用户应该是普通类,则需要实现方法:

class User {
   getRememberToken() {
       return $this->rememberToken;
   }

   setRememberToken($rememberToken) {
      $this->rememberToken = $rememberToken;
   }

   getRememberTokenName() {
      return $this->rememberTokenName;
   }
}