我在Symfony2书中读过
Testing,但没有找到任何有用的,所以,我正在我的应用程序中为控制器创建测试,这是我试图测试的控制器(只是相关代码):
public function createCompanyAction(Request $request) { $response = array(); $response["success"] = false; try { if (statement) { // do the magic here $response["success"] = true; } else { $response['errors'] = "some error"; } } catch (Exception $ex) { $response["exception"] = $ex->getMessage(); } return new JsonResponse($response); }
只有当$response在成功密钥中具有TRUE值时,测试才会通过,但我不知道如何从我的测试控制器中检查它.这是我的代码:
$client->request('POST','/create-company',$data); $response = $client->getResponse(); $this->assertEquals(200,$client->getResponse()->getStatusCode(),'HTTP code is not 200'); $this->assertTrue($response->headers->contains('Content-Type','application/json'),'Invalid JSON response'); $this->assertNotEmpty($client->getResponse()->getContent());
我怎么检查这个?
我回答自己.搜索谷歌我找到了JsonResponse
tests,我发现了如何测试它,所以我将我的代码转换为:
原文链接:https://www.f2er.com/php/134794.html$client->request('POST',$data); $response = $client->getResponse(); // Test if response is OK $this->assertSame(200,$client->getResponse()->getStatusCode()); // Test if Content-Type is valid application/json $this->assertSame('application/json',$response->headers->get('Content-Type')); // Test if company was inserted $this->assertEquals('{"success":"true"}',$response->getContent()); // Test that response is not empty $this->assertNotEmpty($client->getResponse()->getContent());
我还没有测试但它可能有用.