繁体   English   中英

如何模拟 laravel 助手 function 请求

[英]How to mock laravel helper function request

你如何模拟 request() 助手 function?

例如,如果我有一个拨打电话的 function

$name = request()->input('name')

如何模拟该调用,我尝试了以下方法,但我收到一个错误,它从未被调用

$requestMock = $this->mock(Request::class);
$requestMock->shouldReceive('input')->once()->andReturn($name);

If you look at the request() helper ( https://github.com/laravel/framework/blob/05da44d6823c2923597519ac10151f5827a24f80/src/Illuminate/Foundation/helpers.php#L602 ) it's retrieving an instance of the Request class from the Container. 设置模拟请求 class 并添加您需要将其绑定到容器的期望后:

$this->app->instance('request', $mock);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM