问题
I get phpunit and install it as this link using the simplest way for test purposes.
I just download the phpunit.phar file, chmod & rename & move to /usr/local/bin Then, I run phpunit --version, its ok.
PHPUnit 3.7.27 by Sebastian Bergmann
I write a simple test
public function testSomething(){ $this -> assertTrue(true) }
Then I go into the source file folder,
phpunit --colors Test
It works. So, I decide write a complex demo.
My project folder structure is like this.
Project Name --> app --> api --> tests
Now I write a simple class in
app/api/FlyApi.php
<?php class FlyApi { public function makeFly(){ //do something else } }
Then I write another test class for FlyApi.php
<?php class FlyApiTest extends PHPUnit_Framework_TestCase { public function testFly(){ //new a FlyApi $flyApi = new FlyApi(); //do something } }
At this line
$flyApi = new FlyApi()
I got the error.PHP Fatal error: Class 'FlyApi' not found in /home/kevin/Workspace/fly/app/api/FlyApi.php on line 23
Yes, this line
$flyApi = new FlyApi()
回答1:
You didn't load the definition of your FlyApi
class.
This solution is Laravel specific:
You should be extending TestCase
rather than PHPUnit_Framework_TestCase
.
回答2:
Try adding your /api/
folder into your ClassLoader
at app\start\global.php
.
You will find this section:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
...
app_path().'/api/
));
回答3:
Are you using Laravel's phpunit.xml file? It includes Laravel's (and Composer's) autoload.php
file which lets you use all your autoloaded classes within it.
Finally, what's the whole error? It should (hopefully) tell you what class it's trying to load (which will give you clues if the namespace is wrong or something).
来源:https://stackoverflow.com/questions/18942487/phpunit-cannot-find-class-php-fatal-error