请教一个TDD的问题啊

我的Yii配置的数据库连接工作是正常的,可是我想试一下phpunit,就录入了一个用例:

test/unit/DbTest.php




<?php

class DbTest extends CTestCase

{

  function testConnection(){

    $this->assertNotEquals(NULL, Yii::app()->db);

  }

}

?>



run: phpunit unit/DbTest

结果却是:


There was 1 error:


1) DbTest::testConnection

CDbException: CDbConnection failed to open the DB connection.


/Applications/MAMP/htdocs/yii/framework/db/CDbConnection.php:293

/Applications/MAMP/htdocs/yii/framework/db/CDbConnection.php:260

/Applications/MAMP/htdocs/yii/framework/db/CDbConnection.php:238

/Applications/MAMP/htdocs/yii/framework/base/CModule.php:372

/Applications/MAMP/htdocs/yii/framework/base/CModule.php:86

/Applications/MAMP/htdocs/blog/protected/tests/unit/DbTest.php:5


FAILURES!

Tests: 1, Assertions: 0, Errors: 1.

请问一下大家,这是为什么呢?

Me too.我也是一样。I am trying to learn to use yii. And I got stuck at DbTest.php:

C:\inetpub\wwwroot\medappt\trackstar\protected\tests>phpunit unit/DbTest.php

PHPUnit 3.5.13 by Sebastian Bergmann.

E

Time: 1 second, Memory: 5.00Mb

There was 1 error:

  1. DbTest::testConnection

CDbException: CDbConnection failed to open the DB connection.

C:\inetpub\wwwroot\medappt\yii\framework\db\CDbConnection.php:354

C:\inetpub\wwwroot\medappt\yii\framework\db\CDbConnection.php:297

C:\inetpub\wwwroot\medappt\yii\framework\db\CDbConnection.php:275

C:\inetpub\wwwroot\medappt\yii\framework\base\CModule.php:372

C:\inetpub\wwwroot\medappt\yii\framework\base\CModule.php:86

C:\inetpub\wwwroot\medappt\trackstar\protected\tests\unit\DbTest.php:6

FAILURES!

Tests: 1, Assertions: 0, Errors: 1.

======================================================================

main.php:

	/*


	'db'=&gt;array(


		'connectionString' =&gt; 'sqlite:'.dirname(__FILE__).'/&#46;&#46;/data/testdrive.db;',


	),


	*/


	


	'db'=&gt;array(


		'connectionString' =&gt; 'mysql:host=127.0.0.1;dbname=trackstar_dev',


		'emulatePrepare' =&gt; true,


		'username' =&gt; 'doctor',


		'password' =&gt; 'doc3141',


		'charset' =&gt; 'utf8',


	),

=======================================================================

yii requirement:

PDO MySQL extension

Passed

All DB-related classes

This is required if you are using MySQL database.

========================================================================

I am using Vista and IIS 7. PHP and MySQL are newly installed.

I have same error also until I read the CDbConnection.php:

From 642 to 651:

/**


 * Returns the name of the DB driver


 * @return string name of the DB driver


 */


public function getDriverName()


{


	if((&#036;pos=strpos(&#036;this-&gt;connectionString, ':'))&#33;==false)


		return strtolower(substr(&#036;this-&gt;connectionString, 0, &#036;pos));

650 //return $this->getAttribute(PDO::ATTR_DRIVER_NAME);

}

Please check line 650.

What is wrong?

Who put the dumb remark over there?

Who has cost me half day to track it out.

And please someone pay me back during that time.

This dumb remark makes getDriverName() don’t return anything. So the yii cannot load the proper PDO driver. So no one can login the database.

change console.php setting file.


class DbTest extends CDbTestCase

用到DB的话要从CDbTestCase类继承