问题
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.
回答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!
来源:https://stackoverflow.com/questions/22466029/codeception-api-testing-response-comes-back-as-n-a