我正在尝试学习单元测试的基础知识,我正在使用 phpunit 和 laravel
这是我的控制器
胖控制器.php
class FatController extends BaseController {
public $result;
public function __construct($result)
{
$this->result = $result;
}
public function getResult()
{
return $this->$result;
}
}
和测试班
FatControllerTest.php
class FatControllerTest extends TestCase {
/**
* A basic functional test example.
* @return void
*/
public $test;
public function setUp()
{
parent::setUp();
$this->test = new FatController('100%');
}
public function testResult()
{
$result = $this->test->getResult();
$this->assertTrue($result == '100%', 'message');
}
}
据我所知,当测试运行时,它应该创建一个新变量 $test ,它等于 FatController 的新实例,值为 100%
然后它运行 $test FatController Method getResult 应该返回 $result 已由构造函数定义为 '100%'
但是当测试运行时我得到了这个
PHP Warning: Uncaught exception 'ErrorException' with message 'Undefined variable: result' in /Users/irish/Dropbox/www/youtube/app/controllers/FatController.php:15
Stack trace:
#0 /Users/irish/Dropbox/www/youtube/app/controllers/FatController.php(15): Illuminate\Exception\Handler->handleError(8, 'Undefined varia...', '/Users/irish/Dr...', 15, Array)
#1 /Users/irish/Dropbox/www/youtube/app/tests/FatControllerTest.php(22): FatController->getResult()
#2 [internal function]: FatControllerTest->testResult()
#3 /Users/irish/.composer/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php(983): ReflectionMethod->invokeArgs(Object(FatControllerTest), Array)
#4 /Users/irish/.composer/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php(838): PHPUnit_Framework_TestCase->runTest()
#5 /Users/irish/.composer/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php(648): PHPUnit_Framework_TestCase->runBare()
#6 /Users/irish/.composer/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php(783): PHPUnit_Framework_TestResult->run(Object(Fat in /Users/irish/Dropbox/www/youtube/app/controllers/FatController.php on line 15
PHP Fatal error: Cannot access empty property in /Users/irish/Dropbox/www/youtube/app/controllers/FatController.php on line 15
所以我知道 $result 没有被定义为任何东西,当 getResult 尝试返回它时会导致错误,但我不知道我做错了什么