我只是想对我的PHP应用程序实施单元测试,实际上无法弄清它如何与模拟/存根和Codeception一起工作
所以我正在使用Laravel,并希望将测试添加到这样的API端点
$I->sendGET('/user/inventory/<user_id>');
然后在控制器中,我具有对用户进行身份验证的功能,例如
public function getUserInventory($userId) {
if ($this->authenticateUser($userId))
{
do some logic...
if ($success) {
return $good_data;
} else {
return $bad_data;
}
}
return false;
}
authenticateUser
函数与测试无关,我只想模拟对true
或false
的返回,因此我可以测试是否应该返回{{1 }}或$good_data
,有可能吗?
我试图在调用$bad_data
这样的函数之前声明存根对象
sendGET
但是当我运行测试时,它似乎并没有在Stub::make(
'App\Http\Controllers\Controller',
['authenticateUser' => true],
$this
);
$I->sendGET('/user/inventory/<user_id>');
函数中使用存根对象。
在getUserInventory
测试中可以使用存根/模拟方法吗?
希望我的问题有意义