我正在尝试使用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');