如何处理相关的fixture

How deal with related fixtures

我正在使用 phpunit 来测试我的 CakePHP 应用程序,但是,我已经创建了两个装置:- ABA 首先插入,然后插入 B,但是 B 也有一个 a_id 字段,我需要从第一个夹具中填充它。

我的意思的一个很好的例子存在于文档中:https://book.cakephp.org/3.0/en/development/testing.html#loading-fixtures-in-your-test-cases 其中显示了 articlescomments 固定装置,但从未显示 comments 是如何建成。

如何使用 CakePHP 固定装置执行此操作?

灯具彼此不相关。您需要在它们包含的数据中手动建立关联。夹具只是测试中使用的数据 as the book explains it as well。数据将被插入到测试数据库中,无论它们是否相关。只有当您对它生成的数据库表施加约束并且 FK 不存在时,它们才会失败。

When testing code that depends on models and the database, one can use fixtures as a way to generate temporary data tables loaded with sample data that can be used by the test. The benefit of using fixtures is that your test has no chance of disrupting live application data. In addition, you can begin testing your code prior to actually developing live content for an application.

因此,如果您有评论,请在此处使用您文章的 FK 创建记录。