2017-07-18 7 views
0

私はこの問題を何時間も戦ってきており、克服できません。私はYii2と(もちろん)Codeceptionを使ってAPIテストを実行したい。ここに私のapi.suite.ymlYii2でCodeception APIテストを実行

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]); 

結果は以下のログに記録されています。問題は、テストがモバイル/モバイルモジュールではないルートプロジェクトにあるサイト/インデックスを呼び出していることです。モジュールのトレースが呼び出されていないのを見ることができないので、間違ったURLをどこかで選んでいることがわかります。私はブラウザでURLをしようとした場合には、 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..... 

答えて

0

結果: は、私はそれまでに参照し、設定ファイルを変更し、テスト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: 

を使用するようにsuite.api.yamlを変更きれいなURLを含めるには、text-index(config/test.php):

<?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, 
]; 

テストは正常に実行されていました!

関連する問題