我是新来的Codeception,我试图测试我使用Laravel 5构建的Web服务。我遵循指南here。如何使用Codeception测试laravel 5 API?
因此,我创建了一套名为API第一:
codecept generate:suite api
api.suite.yml
class_name: ApiTester
modules:
enabled:
- REST:
url: http://localhost:8000/api/
depends: Laravel5
AuthenticateUserCept.php
<?php
$I = new ApiTester($scenario);
$I->wantTo('authenticate a user');
$I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded');
$I->sendPOST('/authenticate', [
'username' => 'archive',
'email' => '[email protected]',
'password' => 'password'
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
我已经尝试这使用Post男人,它运行良好。 /authenticate
路由采用3个参数并高兴地吐出一个JWT令牌,但我无法使其与Codeception一起工作。
1) Failed to authenticate a user in AuthenticateUserCept (tests/api//AuthenticateUserCept.php)
Step I see response code is 200
Fail Failed asserting that 500 matches expected 200.
Scenario Steps:
3. $I->seeResponseCodeIs(200)
2. $I->sendPOST("/authenticate",{"username":"archive","email":"[email protected]","password":"password"})
1. $I->haveHttpHeader("Content-Type","application/x-www-form-urlencoded")
我在哪里出错了?此外,对于我来说,我如何重构这个特定的测试,以便为我提出的其他请求建立JWT。任何帮助表示赞赏。
在api.suite.yml
class_name: ApiTester
modules:
enabled:
- REST:
url: http://localhost:8000/api/
depends: Laravel5
config:
Laravel5:
environment_file: .env.testing
谢谢你的精彩见解。所以我这次继续将POST请求改为'/ api/authenticate'。所以我挖掘了测试的输出。我想出了一个中间件,它检查令牌以评估有效负载是否搞乱了这些,但中间件不应该由于明显的原因而对'/ api/authenticate'进行操作。但是当我评论Kernal的中间件时,它运行良好。我该如何解决这个问题? – Rohan
将这个到您的api.suite.yml: 配置: Laravel5: environment_file:.env.testing 在您的文件.env.testing,确保您已设置: APP_ENV =测试 否则应该和你的.env文件一样。 默认情况下,当APP_ENV =测试中间件没有运行时。您可以根据具体情况重新启用它,但应该可以解决您的问题。 – delatbabel
我不确定我是否掌握了所有内容。我更改了配置文件,我甚至会编辑我的答案以包含更改。我创建了.env.testing并将'APP_ENV'设置为测试。但是我不明白你对中间件的意思。测试仍然失败。试图解析它不应该使用的令牌的中间件。一旦我评论Kernal中的中间件,它就通过了,但这不是我想要解决的问题。中间件查看请求是否为'api/authenticate',然后跳过。也许codeception不执行HTTP请求?它在另一个中做到了。我很困惑。 – Rohan