lundi 2 novembre 2015

How to use Codeception to test laravel 5 APIs?

I am new to Codeception and I am trying to test my web service which I have built using Laravel 5. I am following the guide here.

So I created a suite called api first:

codecept generate:suite api

api.suite.yml

class_name: ApiTester
modules:
    enabled:
        - REST:
            url: http://localhost:8000/api/
            depends: Laravel5

AuthenticateUserCept.php

<?php 
$I = new ApiTester($scenario);
$I->wantTo('authenticate a user');
$I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded');
$I->sendPOST('/authenticate', [
    'username' => 'archive',
    'email' => 'admin@admin.com',
    'password' => 'password'
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();

I have already tried this using Postman and it runs fine. The /authenticate route takes the 3 parameters and spits out a JWT token happily but I cannot make it work with Codeception.

1) Failed to authenticate a user in AuthenticateUserCept (tests/api//AuthenticateUserCept.php)

 Step  I see response code is 200
 Fail  Failed asserting that 500 matches expected 200.

Scenario Steps:

 3. $I->seeResponseCodeIs(200)
 2. $I->sendPOST("/authenticate",{"username":"archive","email":"admin@admin.com","password":"password"})
 1. $I->haveHttpHeader("Content-Type","application/x-www-form-urlencoded")

Where am I going wrong? And moreover it is still blurry to me that how do I refactor this particular test in order to have a JWT for other requests that I make. Any help is appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire