[英]PHPUnit: Testing RestFul API with die response
我正在嘗試PHPUnit在我的ZF2項目中測試一個動作。
API使用die($jsonObject)
返回JSON字符串。
這將導致PHPUnit測試停止處理,並且只會終止JSON響應。
public function testStartupAction() {
$this->dispatch('/api/v3/startup', "POST", array());
$this->assertJson('{"status":false,"message":"Please enter the UID","data":null}');
}
無法達到assetJson
方法。
PHPUnit輸出:
PHPUnit 4.7.3 by Sebastian Bergmann and contributors.
..{"status":false,"message":"Please enter the UID","data":null}
PHPUnit如何繼續測試,以便得到結果?
我強烈建議嘗試在重寫代碼的過程中找到解決方案,該代碼將在控制器內返回JSON字符串。 使用die
返回響應對象不是走的路。
關於如何從控制器操作中返回JSON的示例可能很簡單:
$model = new \Zend\View\Model\JsonModel($data);
return $model
其中$ data是您的數據數組。 結果將自動更改為JSON字符串。
這也將解決您的測試問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.