I start to write unittests for controllers in cakephp 2.5 and struggle with a testmethod for the the delete method.
Controller:
public function delete($id = null)
{
$this->Content->id = $id;
if (!$this->Content->exists()) {
throw new NotFoundException(__('Invalid content'));
}
$this->Content->recursive = -1;
$content = $this->Content->findById($id);
debug($content);
if ($this->Content->delete($id)) {
$this->Session->setFlash(__('Content deleted'), 'flash_success');
return $this->redirect(array('action' => 'search'));
} else {
$this->Session->setFlash(__('Content was not deleted'), 'flash_error');
return $this->redirect(array('action' => 'view', $id));
}
}
Test:
$this->testAction(
'/contents/delete/11');
$this->assertContains('/contents/search', $this->headers['Location']);
However the $this->headers['Location'] is just myDomain.com instead of myDomain.com/contents/search although the Content with id 11 is in my testDB. So of course my test fails. By the way deleting in my real application works, so I guess the problem is the assertion and not the code of my app.
What would be the correct approach for a test of the delete method?