Question

I found a test that fails when run from a browser if you are logged in. I need to make sure the session is cleared out before running this test. Is there an easy way to clear the CakePHP session from within a test?

I tried calling Session::Destroy(); but the test cannot seem to get visibility of Session.

Maybe I am trying to import Session incorrectly? I am using: App::import('Core', 'Session');

Was it helpful?

Solution 2

I believe gustavotkg is correct. We should be using mock objects. Unfortunately, it will not be completely straight forward to do this the way the code was set up. I will have to go back and rework some things. For a temporary fix this is what I did:

$appController = ClassRegistry::init('AppController');
$appController->constructClasses();
$appController->Session->destroy();

OTHER TIPS

You should mock the Session component. Mark Story himself wrote an article showing how to test controllers.

if(App::import('Core','Session')) {
  $session = new CakeSession();
  $session->start();
}

hope this can help you..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top