【问题标题】:Codeception API testing response comes back as 'N/A'Codeception API 测试响应返回为“N/A”
【发布时间】:2014-04-23 08:19:16
【问题描述】:

我正在编写一个使用 Laravel 和 Codeception 作为测试框架的 API。

我无法让 Codeception 返回一个响应代码,我可以从 Codeception 获取该响应代码以及 JSON 响应。

我编写了一个简单的测试来从 GET 请求中获取用户列表。

测试如下:

$I = new ApiGuy($scenario);
$I->wantTo("access API methods if I'm authenticated as a User");

$I->sendGET('users');

$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);

我可以看到结果虽然还可以,但似乎无法获得响应代码。

产生响应的路由如下:

Route::get('users', function() {

    $users = User::all();
    return Response::json($users->toArray(), 200);
});

Codeception 的回应是:

Modules: PhpBrowser, REST, ApiHelper, Laravel4, Db, Filesystem
-----------------------------------------------------------------------------------------------
Trying to only access api methods if i'm authenticated as a user (UserAuthCept.php)       
Scenario:
* I send get "users"
  [Request] GET http://myapp-api.local/users
  [Response] [{"id":1,"firstname":"First","lastname":"User","email":"firstuser@test.com","phone":"","login":0,"status":0,"username":"firstuser","created_at":"2014-03-17 14:55:29","updated_at":"2014-03-17 14:55:29"},{"id":2,"firstname":"Second","lastname":"User","email":"seconduser@test.com","phone":"","login":0,"status":0,"username":"seconduser","created_at":"2014-03-17 14:55:30","updated_at":"2014-03-17 14:55:30"}]
  [Headers] {"date":["Mon, 17 Mar 2014 17:50:22 GMT"],"server":["Apache"],"x-powered-by":["PHP\/5.4.26-1~dotdeb.0"],"cache-control":["no-cache"],"x-frame-options":["SAMEORIGIN"],"set-cookie":["laravel_session=eyJpdiI6Im9PZ09qYzdZSWI2bnRsTXFxOUJBelFRVUpEVXFKZVp3VFlMU1h1c3lkRG89IiwidmFsdWUiOiJwSDdCVzlMSjU5SERwWmNENzBsOUFaRExXbit3SUcxSG9vRmpRcGN2cXNrK2kzVU1NT0FaTGdsNGZObG1NT01nN01QdlZXU2FCdGpPcjMzY0dJak1hdz09IiwibWFjIjoiOWRmZDEwNGVhNWQ5MWQyMmRiMTBiOWVjNGNkYjA4ZmFlYzg4NjBmYjhjM2Q2ZmRlNWQ3NzlkY2I0NDhlOTVkYiJ9; expires=Mon, 17-Mar-2014 19:50:22 GMT; path=\/; httponly"],"vary":["Accept-Encoding"],"transfer-encoding":["chunked"],"content-type":["application\/json"]}
  [Status] 200
* I see response is json 
* I see response code is 200
 FAIL 

-----------------------------------------------------------------------------------------------


Time: 1.05 seconds, Memory: 9.50Mb

There was 1 failure:

---------
1) Failed to only access api methods if i'm authenticated as a user in UserAuthCept.php
Sorry, I couldn't see response code is 200:
Failed asserting that 'N/A' matches expected 200.

Scenario Steps:
3. I see response code is 200
2. I see response is json 
1. I send get "users"


FAILURES!
Tests: 1, Assertions: 2, Failures: 1.

【问题讨论】:

  • 我可以看到“N/A”的来源:protected function getResponseStatusCode() { // depending on Symfony version $response = $this->client->getInternalResponse(); if (method_exists($response, 'getStatus')) return $response->getStatus(); if (method_exists($response, 'getStatusCode')) return $response->getStatusCode(); return "N/A"; } 所以我尝试添加 Symfony2 模块,但现在说模块要求不满足。

标签: api laravel response codeception


【解决方案1】:

在跟踪代码并查看 seeResponseCodeIs 的所有实例后,意识到它正在执行“框架”版本。取出 Laravel4 模块,它现在可以工作了!

【讨论】:

  • 我希望有一个不涉及删除 Laravel4 模块的解决方案 - 在对我的 API 运行测试之前,我使用它来设置环境、过滤器等。
  • 如果其他人遇到这个问题,我通过将套件配置中的 Laravel4 模块移动到数组的开头来解决这个问题 - 看起来它需要在 REST 模块之前加载。
猜你喜欢
  • 1970-01-01
  • 2022-08-04
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
相关资源
最近更新 更多