【问题标题】:how to test redirect in fuelphp如何在fuelphp中测试重定向
【发布时间】:2015-08-27 05:40:07
【问题描述】:

我这样写一个控制器

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());

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

【问题讨论】:

    标签: php redirect phpunit fuelphp


    【解决方案1】:

    在我的理解中,你不能这样写测试。

    因为Response::redirect()不返回任何内容,而只返回用于重定向的HTTP头,并调用exit()。所以你的 phpunit 测试被exit() 中止了。

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

    【讨论】:

      猜你喜欢
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      相关资源
      最近更新 更多