How to use Codeception to test laravel 5 API?

I am new to Codeception and I am trying to test my web service that I created using Laravel 5. I am following the guide here .

So, I first created the api package:

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 already tried this using Postman and it works fine. The route /authenticatetakes 3 parameters and successfully pours out the JWT token, but I cannot get it to 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 mistaken? And besides, it’s still unclear to me how to reorganize this particular test in order to have JWT for other requests that I make. Any help is appreciated.

EDIT changes in api.suite.yml

class_name: ApiTester
modules:
    enabled:
        - REST:
            url: http://localhost:8000/api/
            depends: Laravel5
    config: 
        Laravel5:
            environment_file: .env.testing
+4
1

, :

  • api.suite.yml : enabled: [PhpBrowser, REST, ApiHelper, Asserts].
  • URL- REST API, . phpbrowser , HTTP-.
  • PhpBrowser Laravel5 - , Laravel5 , APP_ENV .

, , , , , 500. , , / . - :

$I- > seeResponseContains ( '');

, , tests/_output, , 500 (, , 200, ).

, , . , , 500, . , .

, . Laravel5 . , , , API.

0

Source: https://habr.com/ru/post/1614126/


All Articles