PHPUnit_Extensions_Database_Testcase返回未定义的方法

时间:2014-09-17 22:38:59

标签: php mysql pdo phpunit tdd

我得到致命错误:在phpunit测试中调用未定义的方法PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection :: prepare(),我正在准备一个PDO语句。如果默认数据库连接是成功连接的pdo对象的副本,它是否可以访问其方法?

我的班级和有问题的职能:

class User
{ 
protected $db; 

public function __construct($db) 
{ 
$this->db = $db; 
} 

public function deleteItem($itemId)
{
$sql = "
DELETE FROM Users WHERE id = ?";
$sth = $this->db->prepare($sql);//this is the failed line works on other tests
return $sth->execute(array($itemId));
}

我的测试:

class RosterDBTest extends PHPUnit_Extensions_Database_Testcase 
{ 

public function getConnection() 
{
 $pdo = new PDO('mysql:host=localhost;dbname=users','root','root');
return $this->createDefaultDBConnection($pdo,"users"); 
} 

public function getDataSet()  
{  
return $this->createFlatXMLDataset(  
dirname(__FILE__) . '/users.xml');  
}  

public function setup()
{
$this->db = $this->getConnection();
}

public function testRemoveUser()
{
$testUser = new User($this->db);
$expectedUsers = 123;
$testUser->deleteItem(91);
$totalUsers = $testUsers->getAllUsers();
$this->assertEquals( $expectedUsers,count($totalUsers), 'Did not delete User 91' );
} 

1 个答案:

答案 0 :(得分:1)

我刚刚遇到类似的问题,我的抽象数据库测试用例类。通过改变

解决了这个问题
public function setup()
{
  $this->db = $this->getConnection();
}

public function setup()
{
  $this->db = $this->getConnection()->getConnection();
}

不确定这是否对此有所帮助,但希望能帮到某人。