使用 DrupalUnitTestCase 进行 Drupal 单元测试在数据库设置上失败

发布于 2024-11-08 12:06:38 字数 782 浏览 2 评论 0原文

使用 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 &#039;td_development.simpletest50921registry&#039; doesn&#039;t exist: SELECT
filename FROM {registry} WHERE name = :name AND type = :type; Array
(
    [:name] =&gt; FooBar
    [:type] =&gt; interface
)

DrupalUnitTestCaseDrupalWebTestCase 相反,根据设计不设置数据库。那么失败的原因就很清楚了。

然而,当我只想创建一些实例时,我不希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-11-15 12:06:38

你可能不能。

使用 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.

月隐月明月朦胧 2024-11-15 12:06:38

您也可以尝试这个,

spl_autoload_register('your_function');
if (function_exists('drupal_autoload_class')) {
  spl_autoload_unregister('drupal_autoload_class');
  spl_autoload_register('drupal_autoload_class');
  spl_autoload_unregister('drupal_autoload_interface');
  spl_autoload_register('drupal_autoload_interface');
}

这会将 Drupal 自动加载移动到底部并解决问题。

You can also try this

spl_autoload_register('your_function');
if (function_exists('drupal_autoload_class')) {
  spl_autoload_unregister('drupal_autoload_class');
  spl_autoload_register('drupal_autoload_class');
  spl_autoload_unregister('drupal_autoload_interface');
  spl_autoload_register('drupal_autoload_interface');
}

This will move the Drupal autoload to the bottom and solve the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文