使用 DrupalUnitTestCase 进行 Drupal 单元测试在数据库设置上失败
使用 DrupalUnitTestCase
对 Drupal 模块进行单元测试失败。我可能忘记了什么。
测试运行良好,直到我创建某个类的实例:
$foo = new FooBar();
在这种情况下,Drupal 决定施展一些魔法并尝试调用数据库,以便在其注册表中查找某个文件。
Test PDOStatement->execute() failed: <em class="placeholder">PDOException</em>: SQLSTATE[42S02]: Base table [error]
or view not found: 1146 Table 'td_development.simpletest50921registry' doesn't exist: SELECT
filename FROM {registry} WHERE name = :name AND type = :type; Array
(
[:name] => FooBar
[:type] => interface
)
DrupalUnitTestCase
与 DrupalWebTestCase
相反,根据设计不设置数据库。那么失败的原因就很清楚了。
然而,当我只想创建一些实例时,我不希望 Drupal 去查找数据库。如何避免 Drupal 在其注册表中查找该文件?
Using DrupalUnitTestCase
to unit test a Drupal module, fails. I probably forget something.
The test runs fine untill I create an instance of some class:
$foo = new FooBar();
In that case, Drupal decides to do some magic and attempts to call the database, in order to find some file in its registry.
Test PDOStatement->execute() failed: <em class="placeholder">PDOException</em>: SQLSTATE[42S02]: Base table [error]
or view not found: 1146 Table 'td_development.simpletest50921registry' doesn't exist: SELECT
filename FROM {registry} WHERE name = :name AND type = :type; Array
(
[:name] => FooBar
[:type] => interface
)
DrupalUnitTestCase
, as opposed to DrupalWebTestCase
do not set up a database, by design. So the reason why this fails is clear.
However, I don't want Drupal to go looking in a database when all I want is to create some instance. How to avoid Drupal looking up the file in its registry?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可能不能。
使用 UnitTestCase 作为父类的可能性非常有限。一旦您执行任何需要数据库的操作(创建新类就需要数据库,因为 Drupal 7 的自动加载功能依赖于数据库),您就必须使用 WebTestCase。
唯一可能有效的方法是显式包含该类工作所需的所有文件。因为仅当该类尚不存在(也可能是您的类使用或依赖的类)时才会调用自动加载。但这相对脆弱,您始终必须以正确的顺序手动包含所有这些文件,这意味着您的单元测试取决于类的内部工作原理。这也不好。
You probably can't.
The possibilities of using UnitTestCase as the parent class are very limited. As soon as you do anything that requires the database (and creating a new class does because the autoload features of Drupal 7 depend on the database), you have to use WebTestCase.
The only thing that might work is explicitly including all files that are required for that class to work. Because the autoload is only called if the class does not exist yet (could also be a class that your class uses or depends on). But that is relatively fragile and you will always have to include all these files manually in the correct order, which means that your unit tests depend on the inner workings of your class. Which isn't nice either.
您也可以尝试这个,
这会将 Drupal 自动加载移动到底部并解决问题。
You can also try this
This will move the Drupal autoload to the bottom and solve the problem.