【问题标题】:While run phpunit getting error as follows运行 phpunit 时出现如下错误
【发布时间】:2014-06-01 13:13:12
【问题描述】:

请帮我解决这个问题

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

【问题讨论】:

  • 显示你的代码来检查你哪里错了

标签: phpunit


【解决方案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

【讨论】:

    【解决方案2】:

    错误消息已经为您指明了正确的方向:

    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();
    }
    

    现在你需要做什么取决于你想做什么。

    如果 User 应该是一个抽象类:

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

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

    class User {
       getRememberToken() {
           return $this->rememberToken;
       }
    
       setRememberToken($rememberToken) {
          $this->rememberToken = $rememberToken;
       }
    
       getRememberTokenName() {
          return $this->rememberTokenName;
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-08
      • 1970-01-01
      • 2014-09-30
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多