代码之家  ›  专栏  ›  技术社区  ›  Stefano Mtangoo

使用Yii2运行Codeception API测试

  •  2
  • Stefano Mtangoo  · 技术社区  · 8 年前

    api.suite.yml

    class_name: ApiTester
    modules:
        enabled:
            - REST:
                url: /mobile
                depends: Yii2
                part: Json
            - \Helper\Api
        config:
            Yii2:
                entryUrl: http://localhost:8080/index-test.php
    

    和我的测试文件 UserLoginCept.php

    <?php 
    $I = new ApiTester($scenario);
    $I->wantTo('Test User Login');
    $I->sendPOST('mobile/login', ['username' => 'uname', 'password' => '123456']);
    $I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
    $I->seeResponseContainsJson(['success'=>true]);
    

    http://localhost:8080/index.php/mobile/api/login

    {
    
        "success": false,
        "token": ""
    
    }
    

    有人能帮我找出我做错了什么吗?我读了很多书,但找不到这个问题。

    Codeception结果

    $~ codecept --debug run api
    Codeception PHP Testing Framework v2.2.10
    Powered by PHPUnit 4.8.35 by Sebastian Bergmann and contributors.
    
      Rebuilding ApiTester...
    
    Api Tests (1) -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Modules: REST, Yii2, \Helper\Api
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    UserLoginCept: Test User Login
    Signature: UserLoginCept
    Test: tests/api/UserLoginCept.php
    Scenario --
     I send post "/mobile/api/login",{"username":"uname","password":"123456"}
      [Request] POST /mobile/mobile/api/login {"username":"uname","password":"123456"}
      [Request Headers] []
      [yii\db\Connection::open] 'Opening DB connection: mysql:host=localhost;dbname=database_name'
     ERROR 
    
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    1) UserLoginCept: Test user login
     Test  tests/api/UserLoginCept.php
    
      [Error] Call to a member function isAdmin() on null  
    
    
    Scenario Steps:
    
     1. $I->sendPOST("/mobile/api/login",{"username":"uname","password":"123456"}) at tests/api/UserLoginCept.php:4
    
    #1  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/View.php:328
    #2  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/View.php:250
    #3  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Controller.php:396
    #4  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Controller.php:382
    #5  /Users/hosanna/Projects/Volcano/WebApp/controllers/SiteController.php:74
    #6  app\controllers\SiteController->actionIndex
    #7  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/InlineAction.php:57
    #8  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Controller.php:156
    #9  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Module.php:523
    #10 /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/web/Application.php:102
    <!DOCTYPE html>
    <html lang="en-US">
    ..... rest of HTML.....
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Stefano Mtangoo    8 年前

    我是这样解决的: 更改了suite.api。yaml使用test-index.php

    class_name: ApiTester
    modules:
        enabled:
            - Yii2 
            - REST:
                url: http://localhost:8080/index-test.php/mobile/
                depends: Yii2
                part: Json
                configFile: 'config/test.php'
            - \Helper\Api
        config:
            Yii2:
    

    然后,我更改了由文本索引(config/test.php)引用的配置文件,以包括漂亮的URL:

    <?php
    $params = require(__DIR__ . '/params.php');
    $dbParams = require(__DIR__ . '/test_db.php');
    
    /**
     * Application configuration shared by all test types
     */
    return [
        'id' => 'basic-tests',
        'basePath' => dirname(__DIR__),    
        'language' => 'en-US',
        'modules' => [
            'mobile' => [
                'class' => 'app\modules\mobile\Module',
            ],
         ],
        'components' => [
            'db' => $dbParams,
            'mailer' => [
                'useFileTransport' => true,
            ],
            'assetManager' => [            
                'basePath' => __DIR__ . '/../web/assets',
            ],
            'urlManager' => [
                'enablePrettyUrl' => true,
                'enableStrictParsing' => false,
                'showScriptName' => true,
                'rules' => [
                    ['class' => 'yii\rest\UrlRule', 'controller' => 'mobile/api'],
                ],
            ],
            'user' => [
                'identityClass' => 'app\modules\mobile\models\User',
            ],        
            'request' => [
                'cookieValidationKey' => 'test',
                'enableCsrfValidation' => false,
                // but if you absolutely need it set cookie domain to localhost
                /*
                'csrfCookie' => [
                    'domain' => 'localhost',
                ],
                */
            ],        
        ],
        'params' => $params,
    ];