yang
yang

Reputation: 183

how to test redirect in fuelphp

I write a controller like this

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

then i want to write phpunit to test it,

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

something wrong with this,it had insert the data in db,but when it run redirect,i can't redirect the page,so the test failed!WHy?What' wrong with this..

Upvotes: 2

Views: 417

Answers (1)

kenjis
kenjis

Reputation: 664

In my understanding, you can't write test like that.

Because Response::redirect() does not return any contents, but returns only HTTP header for redirection, and calls exit(). So your phpunit testing is aborted by the exit().

To test code with Response::redirect(), you have to replace Response::redirect() method with test double somehow.

Upvotes: 2

Related Questions