我在Codeception中遇到了Dbh模块的一些问题。 首先,我想回滚每个测试中的每个数据库操作(当然是在seeInDatabase之后)。所以我的codeception.yml是
paths:
tests: tests
log: tests/_log
data: tests/_data
helpers: tests/_helpers
settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit_Framework_TestSuite
memory_limit: 1024M
log: true
colors: true
coverage:
enabled: true
remote: true
include:
- controllers/*
- models/*
- widgets/*
modules:
enabled: [Db,Dbh]
config:
Db:
dsn: 'mysql:host=localhost;dbname=baxi'
user: 'root'
password: ''
#dump: tests/_data/dump.sql
cleanup: true
Dbh:
cleanup: false
之后指南说把它放在bootstrap中
<?php
\Codeception\Module\Dbh::$dbh = $dbh;
?>
但是如何从Db模块获得$ dbh?最后我得到错误
[Codeception\Exception\ModuleConfig]
Dbh module is not configured!
Transaction module requires PDO instance explicitly set.
You can use your bootstrap file to assign the dbh:
\Codeception\Module\Dbh::$dbh = $dbh
如何正确设置?
答案 0 :(得分:0)
也许您需要在 bootstrap 文件中执行以下操作:
$dbh = new PDO('mysql:host=localhost;dbname=baxi;charset=utf8', 'root', '');
\Codeception\Module\Dbh::$dbh = $dbh;
答案 1 :(得分:0)
您还可以使用codeception.yml
:
\Codeception\Module\Dbh::$dbh = new PDO($settings['modules']['config']['Db']['dsn'], $settings['modules']['config']['Db']['user'], $settings['modules']['config']['Db']['password']);