Codeception请求返回意外响应?

时间:2015-12-13 06:20:08

标签: php laravel laravel-5 codeception

我正在尝试使用Codeception测试我的API,我正在测试每个端点。我的测试看起来像这样:

404 Route Not Found

现在这个测试未能肯定状态是成功的。相反,我看到状态失败,原因回复给我Route::resource('users', 'UserController'); 。但是当我使用Postman发出相同的请求时,它运行得很好,并且在数据库中创建了一个新用户。

我已经检查了routes.php文件,看起来对我很好。我已将用户资源放在其中:

<?php 


$I = new ApiTester($scenario);
$I->wantTo('create a new comment');
$I->useToken();
$I->sendPost('comments', [
    "document_id" => "1",
    "comment_type_id" => "1",
    "user_id" => "1",
    "body" => "Testing"
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['status' => 'success']);
$I->seeInDatabase('comment', [
    "document_id" => "1",
    "comment_type_id" => "1",
    "user_id" => "1",
    "body" => "Testing"
]);

此外,我还有其他测试:

sendPost

这是正在运行并且断言正常。但是当我故意让它错过404 Route Not Found方法中的几个字段来测试适当的验证时,同样的问题 - &gt; actor: Tester paths: tests: tests log: tests/_output data: tests/_data support: tests/_support envs: tests/_envs settings: bootstrap: _bootstrap.php colors: true memory_limit: 1024M extensions: enabled: [Codeception\Extension\RunFailed] modules: config: { Db: { dsn: 'mysql:host=localhost;dbname=carparts', user: homestead, password: secret, dump: tests/_data/dump.sql } } 这很奇怪。其他测试也会发生这种情况。有些人认为路线没有找到,有些人在我错过了一些参数时会抛出异常,以使它们以某种方式通过验证失败。我不确定我在这里做错了什么。

codeception.yml

class_name: ApiTester
modules:
    enabled:
        - Laravel5
        - Db
        - PhpBrowser:
            url: 'http://archive.app/api'
            curl:
                CURLOPT_RETURNTRANSFER: true
        - REST:
            url: /api/
            depends: PhpBrowser
    config:
        Laravel5:
            environment_file: .env.testing

api.suite.yml

class ApiTester extends \Codeception\Actor
{
    use _generated\ApiTesterActions;

   /**
    * Define custom actions here
    */

    public function saveToken($response)
    {
        file_put_contents('tests/api/token', json_decode($response)->token);
    }

    public function saveSuperToken($response)
    {
        file_put_contents('tests/api/superToken', json_decode($response)->token);
    }

    public function saveFakeToken($response)
    {
        file_put_contents('tests/api/fakeToken', json_decode($response)->token);
    }

    public function useToken()
    {
        $I = $this;
        $I->haveHttpHeader('Authorization', 'Bearer ' . file_get_contents('tests/api/token'));
    }

    public function useSuperToken()
    {
        $I = $this;
        $I->haveHttpHeader('Authorization', 'Bearer ' . file_get_contents('tests/api/superToken'));
    }

    public function useFakeToken()
    {
        $I = $this;
        $I->haveHttpHeader('Authorization', 'Bearer ' . file_get_contents('tests/api/fakeToken'));
    }
}

ApiTester.php

[Request headers] {"Authorization":"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImNhcnBhcnRzIiwic3ViIjoxLCJpc3MiOiJodHRwOlwvXC9hcmNoaXZlLmFwcFwvYXBpXC9hdXRoZW50aWNhdGUiLCJpYXQiOjE0NTAwODg5NzMsImV4cCI6MTQ1MDE0Mjk3MywibmJmIjoxNDUwMDg4OTczLCJqdGkiOiIxOTM4YmU0OWZhZjIyMTE5ZjJhZDY0ZDAwOTJhZTExOSJ9.Gsc0Acxq7QPMOejMHRJ4yCYAhxnDUscGqo1-NbT7gIM"}
  [Request] POST /api/users {"first_name":"Test","last_name":"Test","email":"test@test.com","password":"testing","role":"1"}
  [Response] {"status":"error","data":[null],"errors":null,"message":"404 Route Not Found"}
  [Cookies] {"laravel_session":"eyJpdiI6Im03Smh5VUZJMEdJVUVjVDZxRlpzVVE9PSIsInZhbHVlIjoiWHF2aTQ5MXZWbnl2eU5OREc4cDhmOENEeUlGRzRKbVpuYXFQeW9WMlVzQjRUT2hTZldWVnJ4SmVFTEJ3Y1lKblwvQWlWalhWSkVEUW95b0t2QkxkTDJBPT0iLCJtYWMiOiI2N2MwYmM3N2JjZTI5NWE2NjAyMWY3OWI3ZDJkMDQzNGU5ZTk2ODBkNmNiNmRhZWVhOTdlZmEzYWUyNDI4YzA3In0="}
  [Headers] {"Server":["nginx/1.8.0"],"Content-Type":["application/json"],"Transfer-Encoding":["chunked"],"Connection":["keep-alive"],"Cache-Control":["no-cache"],"Date":["Mon, 14 Dec 2015 10:29:55 GMT"]}
  [Status] 200

- 调试选项输出

var newdate = new Date();
var q1age = localStorage.getItem('age');
var q2gender1 = localStorage.getItem('gender1');
var q2gender2 = localStorage.getItem('gender2');

2 个答案:

答案 0 :(得分:0)

你能给我们一个有效的Postman屏幕截图吗? enter image description here

答案 1 :(得分:0)

此问题的可能原因是在api.suite.yml中启用了Laravel5模块

Laravel5(和其他框架模块)与PhpBrowser不兼容,启用这两个是一个非常常见的问题。

seeResponseCodeIs方法由Laravel5,PhpBrowser和REST模块提供 客户端在REST模块和它所依赖的模块之间共享,但很可能是由ApiTester使用Laravel5提供的seeResponseCodeI。

解决方案是从此套件中删除Laravel5模块。