How do I destroy a CakePHP session from inside of a test?
-
27-02-2021 - |
题
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');
解决方案 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();
其他提示
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..