Cakephp 2.0 mock auth

I am developing an application in Cake 2.2 and am currently writing some unit tests. Model tests work fine, I have some problems with controller checks in general.

Situation: - most actions require a user login - there are custom components and libraries of suppliers - I have lights for all tables

Problem: - Many actions use the $ this-> Auth-> user () method to get user data - If I write a test for this action, user data (obviously) does not exist

My (non-working) solution: - I tried to mock the auth component so that it contained a user method and always return a static array containing user data, but it returns null, here is the code:

$Days = $this->generate('Days', array('components' => 'Auth'));
$Days->Auth->expects($this->once())->method('user')->will($this->returnValue(array(..userdata etc.)));
$result = $this->testAction('/days/settings');

It simply says that the method has not been called (although the settings method inside the Days controller calls it exactly once). What is wrong with the layout? Hmmm ..

Any help would be appreciated!

-edit- The code of the tested method:

public function myDays()
{
   $user = $this->Auth->user();
   $days = $this->Day->find('all', array('conditions' => array('user_id' => $user['id'], 'active' => 1)));
   $this->set('days', $days);
}
+5
source share
1 answer

Possible use:

$Days->Auth->staticExpects(

since user () is a static method.

+1
source

All Articles