mardi 2 juillet 2019

How to Mock an External API

I have to write Test Cases for the API Endpoint that I build in Laravel 5.6. The API Endpoint calls an external API and based on the response and the values in Database it takes a decision.

Below is how my Model method which is called by my Endpoint loosely looks like

public function getFields( $user ) {

    $apiResponse = ExternalAPI::getResponse ( $user->id );

    if( $user->perms == 1 ) {
        if( $apiResponse->fields == 'contact' ) {
            return true;
        } else {
            return false;
        }
    } elseif( $user->perms == 2) {
        if( $apiResponse->fields == 'leads' ) {
            return true;
        } else {
            return false;
        }   
    }

    return false;

}

So I have to mock the Response from the API as well as set the appropriate Value in Database before each Test Case.

Could you please advice how would I write different Test Cases for testing this. I understand that I have to use Mocking and Factory Class, but I am not sure how to put it out all together.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2RPWPLQ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire