如何在 fuelphp 中测试重定向

how to test redirect in fuelphp

我写的控制器是这样的

public function action_submit()
{
$submit = Format::forge(json_decode($_POST["submit"]))->to_array();
Servicecode::add_code_request($submit);
Response::redirect('code/codedetail');
}

那我想写phpunit来测试一下,

public function test_adminsubmit()
{
$Submit = array(...);
$_POST["Submit"] = json_encode(Submit);
$response = Request::forge('code/codeeditrequest/submit')
  ->set_method('POST')
  ->execute()
  ->response();
$this->assertContains('ode Detail', $response->body->__toString());

这有问题,它已将数据插入数据库,但是当它 运行 重定向时,我无法重定向页面,所以测试失败了!为什么?这有什么问题..

以我的理解,你不能那样写测试。

因为 Response::redirect() 没有 return 任何内容,但是 return 只有 HTTP header 用于重定向,并调用 exit()。所以你的 phpunit 测试被 exit().

中止了

要使用 Response::redirect() 测试代码,您必须以某种方式将 Response::redirect() 方法替换为测试替身。