如何在 Laravel 的测试控制器中模拟其他 class 函数
How to mock other class functions in testing controller in Laravel
我正在尝试在 Laravel 中测试一个控制器,它使用另一个 class 作为助手,调用 API 和 returns 结果。
为了避免外部 API 调用,我需要模拟这个助手。
我试图模拟控制器内部的 class 和 运行 测试,但我没有得到我在模拟 class.
中预期的结果
这是我的控制器方法:
public function A(Request $request){
$helper = new TheHelper();
$result = $helper->getResult($request->email);
if($result){
return response()->json([
'success' => true,
'message' => "result found",
], 200);
}else{
return response()->json([
'success' => false,
'message' => "no result",
], 500);
}
}
我的辅助方法只是调用 API 和 returns 结果。
class TheHelper
{
public function getResult($email){
// some api calls
return $result;
}
}
这是我的测试:
public function testExample()
{
$helperMock = Mockery::mock(TheHelper::class);
// Set expectations
$helperMock ->shouldReceive('getResult')
->once()
->with('testemail@test.com')
->andReturn([
'id' => '100'
]);
$this->app->instance(TheHelper::class, $helperMock);
$this->json(
'POST',
'/api/test_method',
['email' => 'testemail@test.com'])
->assertStatus(200);
}
我的模拟函数从未调用过。它只检查 TheHelper 方法
中的真实 API
您的测试正在创建一个模拟对象并将该模拟对象绑定到 Laravel 服务容器中。但是,您的控制器并未从 Laravel 服务容器中拉取 TheHelper
实例;它使用 new
关键字手动实例化它。使用new
关键字是核心PHP,根本不涉及Laravel。
您的测试表明您的代码存在问题。 TheHelper
是您的方法的依赖项,因此应该传递到方法中而不是在方法内部创建。
您要么需要更新您的控制器方法以使用依赖项注入,以便 Laravel 可以自动解析其容器中的 TheHelper
依赖项,要么您需要替换您的 new
调用 Laravel 容器的关键字。
使用依赖注入:
public function A(Request $request, TheHelper $helper)
{
$result = $helper->getResult($request->email);
// rest of function...
}
从容器中手动拉取:
public function A(Request $request)
{
$helper = app(TheHelper::class);
$result = $helper->getResult($request->email);
// rest of function...
}
我正在尝试在 Laravel 中测试一个控制器,它使用另一个 class 作为助手,调用 API 和 returns 结果。
为了避免外部 API 调用,我需要模拟这个助手。
我试图模拟控制器内部的 class 和 运行 测试,但我没有得到我在模拟 class.
中预期的结果这是我的控制器方法:
public function A(Request $request){
$helper = new TheHelper();
$result = $helper->getResult($request->email);
if($result){
return response()->json([
'success' => true,
'message' => "result found",
], 200);
}else{
return response()->json([
'success' => false,
'message' => "no result",
], 500);
}
}
我的辅助方法只是调用 API 和 returns 结果。
class TheHelper
{
public function getResult($email){
// some api calls
return $result;
}
}
这是我的测试:
public function testExample()
{
$helperMock = Mockery::mock(TheHelper::class);
// Set expectations
$helperMock ->shouldReceive('getResult')
->once()
->with('testemail@test.com')
->andReturn([
'id' => '100'
]);
$this->app->instance(TheHelper::class, $helperMock);
$this->json(
'POST',
'/api/test_method',
['email' => 'testemail@test.com'])
->assertStatus(200);
}
我的模拟函数从未调用过。它只检查 TheHelper 方法
中的真实 API您的测试正在创建一个模拟对象并将该模拟对象绑定到 Laravel 服务容器中。但是,您的控制器并未从 Laravel 服务容器中拉取 TheHelper
实例;它使用 new
关键字手动实例化它。使用new
关键字是核心PHP,根本不涉及Laravel。
您的测试表明您的代码存在问题。 TheHelper
是您的方法的依赖项,因此应该传递到方法中而不是在方法内部创建。
您要么需要更新您的控制器方法以使用依赖项注入,以便 Laravel 可以自动解析其容器中的 TheHelper
依赖项,要么您需要替换您的 new
调用 Laravel 容器的关键字。
使用依赖注入:
public function A(Request $request, TheHelper $helper)
{
$result = $helper->getResult($request->email);
// rest of function...
}
从容器中手动拉取:
public function A(Request $request)
{
$helper = app(TheHelper::class);
$result = $helper->getResult($request->email);
// rest of function...
}