snowair / think-phpunit

ThinkPHP3.2应用单元测试辅助类
100 stars 32 forks source link

不带use \Think\PhpUnit;时测model会报错 #8

Open robbinhan opened 7 years ago

robbinhan commented 7 years ago

按照文档上写usermodel测试类,起初没有带use \Think\PhpUnit;,是有抛异常的应该


PHP Fatal error:  Uncaught TypeError: Argument 2 passed to PHPUnit_Framework_TestResult::addError() must be an instance of Exception, instance of Error given, called in /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/Framework/TestSuite.php on line 699 and defined in /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/Framework/TestResult.php:195
Stack trace:

#0 /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/Framework/TestSuite.php(699): PHPUnit_Framework_TestResult->addError(Object(PHPUnit_Framework_TestSuite), Object(Error), 0)

Fatal error: Uncaught TypeError: Argument 2 passed to PHPUnit_Framework_TestResult::addError() must be an instance of Exception, instance of Error given, called in /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/Framework/TestSuite.php on line 699 and defined in /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/Framework/TestResult.php:195#1 /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(440): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
Stack trace:
#0 /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/Framework/TestSuite.php(699): PHPUnit_Framework_TestResult->addError(Object(PHPUnit_Framework_TestSuite), Object(Error), 0)

#1 /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(440): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))#2 /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/TextUI/Command.php(149): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#2 /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/TextUI/Command.php(149): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#3 /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/TextUI/Command.ph in /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/Framework/TestResult.php on line 195
#3 /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/TextUI/Command.ph in /Users/robbin/Downloads/thinkphp_3.2.3_full/vendor/phpunit/phpunit/src/Framework/TestResult.php on line 195

带上后测试就没问题

knghlp508 commented 7 years ago

我也发现了,确实要 use \Think\PhpUnit; 不然没法在静态方法setUpBeforeClass的使用self::$app

snowair commented 7 years ago

如果模型要 use \Think\PphpUnit 说明模型设计不好,与业务发生了耦合。

好的模型设计,是与业务解耦,可独立测试的。