laravel - 在 laravel 5 测试期间如何模拟外部 API?

标签 laravel testing laravel-5

我想在 laravel 中测试 HTTP 路由。 URL 的操作函数调用辅助函数,该函数调用外部 API。如何在测试时模拟外部 API 调用?

public function actionFunction(){

     $helper = new HelperClassHelper();

     return Response::json($helper->getNames());

}

此处,getNames() 函数进行外部 API 调用。我如何 mock 它?

最佳答案

您可以将 HelperClassHelper 添加为操作中的依赖项,然后您可以在测试中模拟它:

public function actionFunction(HelperClassHelper $helper){
     return Response::json($helper->getNames());
}

在测试中:

$this->app->bind(HelperClassHelper::class, function () { /* return mock */ });

关于laravel - 在 laravel 5 测试期间如何模拟外部 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52309653/

相关文章:

javascript - 未捕获的类型错误 : callback is not a function while implementing pusher with laravel

laravel - 在 Laravel 中播种大数据的最佳案例

php - 在 Laravel : How do I sort children by name? 中对 child 进行排序

laravel-5 - 如何在laravel 5中返回数组而不是对象

php - Laravel 的最佳 CSV 操作包

php - 拉维尔 5.6 : Create schema in database

php - 下拉菜单选择()不工作 Laravel Dusk

unit-testing - 使用单元测试但不使用 TDD 的编程工作流

testing - 使用 fitnesse 表中的数据作为变量

php - 如何将现有数据库导入宅基地​​?