我正在使用 Silex 中的路由从数据库中删除对象。如果对象不存在,则应抛出 404 错误。这在浏览器中工作正常,并相应地收到响应。
这是我的来源:
$app->delete("/{entity}/{id}", function(\Silex\Application $app, HttpFoundation\Request $request, $entity, $id) {
// some prep code is here
$deleteObject = $this->em->getRepository($entityClass)->find($id);
if (empty($deleteObject))
$app->abort(404, "$ucEntity with ID $id not found");
// other code comes here...
}
这是我的测试用例:
// deleting the same object again should not work
$client->request("DELETE", "/ccrud/channel/$id");
$this->assertTrue($response->getStatusCode() == 404);
现在 phpunit 失败并出现以下错误: 1) CrudEntityTest::testDelete Symfony\Component\HttpKernel\Exception\HttpException: Channel with ID 93 not found
我可以从消息中看到 404 被抛出,但我无法按计划测试响应对象。我知道理论上我可以断言异常本身,但这不是我想要做的,我想获得响应(就像浏览器一样)并测试状态代码本身。
有人知道如何达到这个目标,或者是否有更好的方法来测试这个?
谢谢,丹尼尔