codeception API测试响应返回为“N / A” [英] Codeception API testing response comes back as 'N/A'

查看:177
本文介绍了codeception API测试响应返回为“N / A”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写使用Laravel和codeception作为我的测试框架的API。

我有麻烦codeception回来了回应code,我可以从codeception回暖以及一个JSON响应。

我写了一个简单的测试,从GET请求获得的用户列表。

的试验如下:

  $ I =新ApiGuy($的情况);
$ I-GT&; wantTo(访问API方法,如果我认证为用户);$ I-GT&; sendGET(用户);$ I-GT&; seeResponseIsJson();
$ I-GT&; seeResponse codeIS(200);

我可以看到的结果来确定,虽然,只是不能似乎得到的回应code。

这产生反应路线如下所示:

 路线::得到('用户',函数(){    $用户=用户::所有();
    返回响应:: JSON($用户 - >的toArray(),200);
});

从codeception的响应是:

 模块:PhpBrowser,REST,ApiHelper,Laravel4,DB,文件系统
-------------------------------------------------- ---------------------------------------------
试图只能访问API的方法,如果我作为认证用户(UserAuthCept.php)
场景:
*我发送GET用户
  [请求] GET HTTP://myapp-api.local/users
  [响应] [{\"id\":1,\"firstname\":\"First\",\"lastname\":\"User\",\"email\":\"firstuser@test.com\",\"phone\":\"\",\"login\":0,\"status\":0,\"username\":\"firstuser\",\"created_at\":\"2014-03-17十四点55分29秒,的updated_at:2014年3月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十四时55分30秒,的updated_at:2014年3月17日14点55分三十秒}]
  [标题] {日期:星期一,2014年3月17日17点五十分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;到期=周一,17月 - 2014年19点五十分22秒格林尼治标准​​时间; PATH = \\ /; httponly\"],\"vary\":[\"Accept-Encoding\"],\"transfer-encoding\":[\"chunked\"],\"content-type\":[\"application\\/json\"]}
  [状态] 200
*我看到的反应是JSON
*我看到响应code是200
 失败-------------------------------------------------- ---------------------------------------------
时间:1.05秒时,内存:9.50Mb有1失败:---------
1)未能只能访问API的方法,如果我鉴定为在UserAuthCept.php用户
对不起,我看不出响应code为200:
失败的断言'N / A'匹配预期的200。情景步骤:
3.我看到响应code是200
2.我看到的反应是JSON
1.我送得到用户
FAILURES!
测试:1,断言:2,故障:1。


解决方案

继code,看着seeResponse $ C $独联体的所有实例后,意识到这是执行框架的版本。拿出Laravel4模块,它现在的作品!

I am writing an API using Laravel and Codeception as my testing framework.

I am having trouble getting Codeception to come back with a response code that I can pick up from Codeception as well as a JSON response.

I've written a simple test to get a list of users from a GET request.

The test is as follows:

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

$I->sendGET('users');

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

I can see the results coming though ok, just cant seem to get the response code.

The route that generates the response is as follows:

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

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

The response from Codeception is:

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.

解决方案

After following the code and looking at all instances of seeResponseCodeIs, realised it was executing the 'Framework' version. Took out the Laravel4 module and it now works!

这篇关于codeception API测试响应返回为“N / A”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆