dongzang5815 2019-08-09 16:20
浏览 79

如何使用代码模拟内部测试函数的方法

I'm just trying to implement unit testing to my PHP application and really can't figure out how it works with mock/stub and Codeception

So I'm using Laravel and want to add testing to an API endpoint like this

$I->sendGET('/user/inventory/<user_id>');

Then in the controller I have function to authenticate the user, something like

public function getUserInventory($userId) {

    if ($this->authenticateUser($userId)) 
    {
        do some logic...
        if ($success) {
            return $good_data;
        } else {
            return $bad_data;
        }
    } 
    return false;
}

authenticateUser function is not relevant to the test and I just want to mock the return to true or false so I can test the actual logic on whether it should return $good_data or $bad_data, is that possible?

I've tried to declare stub object before calling the sendGET function like this

Stub::make(
    'App\Http\Controllers\Controller',
    ['authenticateUser' => true],
    $this
);

$I->sendGET('/user/inventory/<user_id>');

but it doesn't seem to use the stub object inside the getUserInventory function when I ran the test.

Is there any way to use stub/mock method inside the $I->sendGET('/user/inventory/<user_id>'); test?

Hopefully my question make sense

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)