我想上手使用MySQL數據庫建立單元測試,我遇到了此異常:PHPUnit的數據庫測試異常
DBTest::test__getException()
Argument 1 passed to PHPUnit_Extensions_Database_DataSet_DefaultTableIterator::__construct() must be an array, null given.
我不知道我可能會錯過
我的單元測試代碼:
<?php
class DBTest extends Generic_Tests_DatabaseTestCase {
//...
public function getDataSet() {
$dataSet = $this->createMySQLXMLDataSet(dirname(__FILE__)."/../db/t_enroll_fixtures.xml");
return $dataSet;
}
public function setUp() {
$this->X = $this->getMock('\X\Engine\X');
$this->model = new Model($this->X, 't_users');
$this->className = get_class($this->model);
parent::setUp();
}
public function tearDown() {
$this->X = NULL;
$this->model = NULL;
parent::tearDown();
}
public function testMagicFields() {
$this->getConnection()->addTable('t_enroll');
$this->assertEquals(10, $this->getConnection()->getRowCount('t_enroll'));
}
}
?>
的Generic_Test_DatabaseTestCase類:
<?php
require_once "PHPUnit/Extensions/Database/TestCase.php";
abstract class Generic_Tests_DatabaseTestCase extends PHPUnit_Extensions_Database_TestCase {
// only instantiate pdo once for test clean-up/fixture load
static private $pdo = null;
// only instantiate PHPUnit_Extensions_Database_DB_IDatabaseConnection once per test
private $conn = null;
final public function getConnection() {
if($this->conn === null) {
if(self::$pdo == null) {
self::$pdo = new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
}
$this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_DBNAME']);
}
return $this->conn;
}
}
?>
我錯過了什麼?
相關問題:http://stackoverflow.com/questions/4640802/php-testing-models-with-zend-test-phpunit-databasetestcase – 2012-03-01 15:09:42
@cillosis這個問題是關於我收到的錯誤消息的專利,但OP的解決方案出現t o非常適合Zend。你能否更多地瞭解我可以如何使用這個問題來幫助我? – 2012-03-01 15:20:38
@LeviHackwith我有同樣的問題,並沒有使用Zend。我已經在我的xml文檔中有數據庫名稱。你有沒有想出一個不同的解決方案,或爲你解決它?謝謝。 – GreeKatrina 2015-03-24 16:16:21