首页 > 解决方案 > Laravel assertDatabaseMissing 奇怪的行为

问题描述

这是我的代码(测试)。此方法中没有其他代码:

$this->withoutMiddleware();  
$fc = factory(\App\Models\FaqCategory::class)->create();
$response = $this->delete('/admin/faq-categories/'.$fc->id);
$response->assertStatus(302);
$response->assertRedirect('/admin/faq-categories/');
$this->assertDatabaseMissing('faq_categories', ['id' => $fc->id]);

我不知道为什么,但最后一个断言失败,即使数据库中不存在行。对象在第 3 行被删除,但第 6 行仍然失败。

编辑:我想通了。我在我的控制器中使用模型绑定,所以我不能只将 $this->withoutMiddleware() 放在测试中,因为路由模型绑定不起作用(需要在中间件中完成一些事情)。

标签: databaselaraveltestingautomated-tests

解决方案


有同样的问题,要么它取决于你的数据库驱动程序支持而不是(sqlite 不支持级联等),要么它很简单(和我的情况一样愚蠢)该项目被软删除。

LARAVEL UNIT TEST - 与seeInDatabase相反

https://laracasts.com/discuss/channels/laravel/super-weird-test-failure


推荐阅读