laravel - Laravel - 在单元测试中伪造保存失败
问题描述
如果我们想要测试以下代码,那么模拟失败保存的最佳方法是什么?目前,我的测试中唯一缺少的部分是else
声明。
保存是循环的一部分,我们$customers
循环遍历它们并执行一些操作。
$customers = Customer::where('created_at', '<=', $start);
$customers->each(function ($customer, $key) {
if ($customer->save()) {
//Do something here
} else {
//Saving failed, log something
}
}
测试的所有数据都来自工厂,并在每次测试时动态生成。
解决方案
好吧,简单但肮脏的事情是通过saving
事件假装保存失败:
这是事件处理程序的注释:
public function save(array $options = [])
{
$query = $this->newModelQuery();
// If the "saving" event returns false we'll bail out of the save and return
// false, indicating that the save failed. This provides a chance for any
// listeners to cancel save operations if validations fail or whatever.
if ($this->fireModelEvent('saving') === false) {
return false;
}
....
因此,类似以下的东西应该起作用:
class TestModelSaving {
public function testSaveFailureLogs() {
// Create the fake model here
// If the event handler for saving returns false then `save()` will return false
Customer::saving(function () { return false; });
// Call your unit under test here
// Cleanup: Usually unnecessary, but some test configurations might need it
Customer::flushEventListeners();
}
}
为了测试是否记录了事情,您可以通过模拟记录器外观Log::shouldReceive(....)
(参数与具有相同名称的模拟函数相同)
推荐阅读
- java - 如何将带有句点(。)的 xml 标记转换为 jaxb 变量
- reactjs - Visual Studio 2017(不是 VS Code)中的 React (jsx) 支持
- javascript - 如何在页面加载时生成随机数?
- java - HashMap get 方法在 hm.get(hm) 上抛出错误
- php - 试图获取非对象的属性“标题”(查看:/opt/lampp/htdocs/commonroom/resources/views/home.blade.php
- node.js - 使用 Axios/Axios-retry 向服务器发送数据的 HTTP 调用
- selenium - 如何处理 Cucumber AmbiguousStepDefinitions 异常?
- javascript - 在phantomjs中读取本地文件
- database - 为什么 cassandra 导入模式较慢
- reactjs - 使用 AWS 后端通过 Next.js/React 登录 JWT