vendredi 9 août 2019

How to mock method inside tested function using codeception

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)) 
    {
        return success;
    } 
    return fail;
}

authenticateUser function is not relevant to the test and I just want to mock the return to true or false, 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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire