database - 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() 放在测试中,因为路由模型绑定不起作用(需要在中间件中完成一些事情)。
解决方案
有同样的问题,要么它取决于你的数据库驱动程序支持而不是(sqlite 不支持级联等),要么它很简单(和我的情况一样愚蠢)该项目被软删除。
看
LARAVEL UNIT TEST - 与seeInDatabase相反
https://laracasts.com/discuss/channels/laravel/super-weird-test-failure
推荐阅读
- java - 带同步的 ConcurrentHashMap
- php - PHP和HTML制作漂亮的交互式文件显示页面
- python - Python pandas:使用条件进行索引
- java - 将电话号码或 CONTACT_ID 从 android 联系方式传递给活动(whatsapp 之类)
- c# - 如何启用 SSMS 以自动刷新架构更改
- python-3.x - 所有 Python dunder 方法的列表 - 您需要实现哪些方法才能正确代理对象?
- java - 使用 DPS 配置后禁用或限制 Azure IOT 设备
- javascript - 如何在 (React Native Axios) 中使用 API POST 请求将数据插入数据库
- blueprism - 如何将工作队列与文件目录同步
- java - 从 Retrivel API 中的消息中提取 OTP(自动短信验证)