[英]Unit-testing with CakePhP and PHPUnit
I`m trying to do unit-testing with CakePhP 2.3 and PHPUnit 2.7. 我正在尝试使用CakePhP 2.3和PHPUnit 2.7进行单元测试。 I want to test the index function in my customer's controller.
我想在客户控制器中测试索引功能。 In my controller I have:
在我的控制器中,我有:
public function index() {
$this->Customer->recursive = -1;
$data = $this->paginate('Customer', array( 'Customer.status'=>'active'));
$this->set('customers', $data);
}
I tried to follow the examples in book.cakephp.org, so I created Fixture class in which I`m importing the Customer schema and all the records. 我尝试遵循book.cakephp.org中的示例,所以我创建了Fixture类,在其中导入了Customer模式和所有记录。
class CustomerFixture extends CakeTestFixture{
public $import = array('model' => 'Customer', 'records' => true);
}
And finally my test class looks like this: 最后,我的测试类如下所示:
class CustomersControllerTest extends ControllerTestCase {
public $fixtures = array('app.customer');
public function testIndex() {
$result = $this->testAction('/customers/index');
debug($result);
}
}
When I run my test I have the following error: 运行测试时,出现以下错误:
Database Error Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '8' for key 'PRIMARY' 数据库错误错误:SQLSTATE [23000]:违反完整性约束:1062键“ PRIMARY”的条目“ 8”重复
Do you have any ideas what can be the problem? 您有什么想法可能是问题吗?
In your database.php in app/Config folder you have to add a $test variable. 在app / Config文件夹中的database.php中,您必须添加$ test变量。
For example 例如
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => true,
'host' => 'localhost',
'port' => 3306,
'login' => 'root',
'password' => 'xxxx',
'database' => 'mydatabase',
'encoding' => 'utf8'
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'port' => 3306,
'login' => 'root',
'password' => 'xxxx',
'database' => 'mydatabase_test',
'encoding' => 'utf8'
);
}
Then your unit testing will use the mydatabase_test for testing your code. 然后,您的单元测试将使用mydatabase_test来测试您的代码。 Because now it uses the default database.
因为现在它使用默认数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.