【发布时间】:2018-05-24 20:23:42
【问题描述】:
我正在尝试使用 phpunit 测试我用 laravel faramework 编写的 REST API。 这是我的 api.php 文件代码
Route::group([
'middleware' => ['cors','db.select'],
'prefix' => 'v1/{database}',
], function() {
Route::resource('test-beds', 'TestBedController');
});
其中 cors 中间件 允许发送跨域资源共享,而 db.select 中间件 由我创建,它从路由中删除了 {database} 变量。
在我的 TestBedController 中,如果我不使用 post 方法发送 id 字段,它会给出 HTTP 状态代码 422 的响应。为了验证我是否编写了以下代码。
这是我的 TestBedApiTest.php 文件
class TestBedApiTest extends TestCase {
public function testRequiresId() {
// I tried by adding and removing following line but no luck
$this->WithoutMiddleware();
$this->json('POST', 'test-beds')->assertResponseStatus(422);
}
}
但它返回了我 404,这很可能是因为它无法找到路线 test-beds。我尝试了以下代码但没有运气
$this->json('POST', 'v1/db1/test-beds')->assertResponseStatus(422);
$this->json('POST', 'api/v1/db1/test-beds')->assertResponseStatus(422);
$this->json('POST', 'api/v1/test-beds')->assertResponseStatus(422);
$this->json('POST', 'v1/{database}/test-beds')->assertResponseStatus(422);
【问题讨论】:
标签: php laravel phpunit laravel-5.3