为什么我收到此错误?致命错误:找不到类'TestCase'

时间:2011-09-29 21:41:36

标签: php phpunit wamp

即使我已经包含TestCase类了吗?

    <?php

require_once("PHPUnit/Autoload.php");
require_once("PHPUnit/Framework/TestCase.php");
require_once("PHPUnit/Framework/TestSuite.php");

class WidgetSession {
    public function __construct($one, $two){}
    public function login() {}
    public function isLoggedIn() {return null;}
    public function getUser(){
        return new WidgetUser();
    }
}

class WidgetUser{
    public $first_name = "";
    public $last_name = "";
    public $email = "";

    public function isSalesPerson() {return null;}
    public function isSalesManager() {return null;}
}

class TestWidgetSession extends TestCase {

    private $_session;
    function setUp(){
        $dsn = array(
            'phptype' => "pgsql",
            'hostspec' => "localhost",
            'database' => "widgetworld",
            'username' => "wuser",
            'password' => "foobar"
        );
        $this->_session = new WidgetSession($dsn, true);
    }

    function testValidLogin(){
        $this->_session->login("ed", "12345");
        $this->assertEqual(true, $this->_session->isLoggedIn());
    }

    function testInvalidLogin(){
        $this->_session->login("ed", "54321"); //fail
        $this->assertEquals(false, $this->_session->isLoggedIn());
    }

    function testUser(){
        $user = $this->_session->getUser();
        $this->assertEquals("Lecky Thompson", $user->last_name);
        $this->assertEquals("Ed", $user->first_name);
        $this->assertEquals("ed@lecky-thompson.com", $user->email);
    }

    function testAuthorization(){
        $user = $this->_session->getUser();
        $this->assertEquals("Sales Person", $user->role);
        $this->assertEquals(true, $user->isSalesPerson());
        $this->assertEquals(false, $user->isSalesManager());
        $this->assertEquals(false, $user->isAccountant());
    }
}

$suite = new TestSuite;
$suite->addTest(new TestWidgetSession("testValidLogin"));
$suite->addTest(new TestWidgetSession("testInvalidLogin"));
$suite->addTest(new TestWidgetSession("testUser"));
$suite->addTest(new TestWidgetSession("testAuthorization"));
$testRunner = new TestRunner();
$testRunner->run($suite);
?>

1 个答案:

答案 0 :(得分:5)

require_once("PHPUnit/Framework/TestCase.php");

此文件肯定不包含TestCase 的任何定义,但 PHPUnit_Framework_TestCase。注意并使用正确的类名。

我认为同样适用于:

require_once("PHPUnit/Framework/TestSuite.php");

PHPUnit_Framework_TestSuite而不是TestSuite

你只需要/包含自动加载器,这对于PHPUnit的类就足够了。