Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp 2.0 mock auth
    text
    copied!<p>I am developing an app in Cake 2.2, and am currently writing some unit tests. The model tests are working fine, i have some issues with the controller tests in general.</p> <p>The situation: - most of the actions require a user login - there are custom components and vendor libraries - I have fixtures for all tables</p> <p>The problem: - Many actions use the "$this->Auth->user()" method to get the user-data - If i write a test for that action, the user-data is (obviously) not existant</p> <p>My (not working) solution: - I tried to mock the auth component, so that it would contain the user-method and always return a static array containing the user data, but it returns null, here's the code:</p> <pre><code>$Days = $this-&gt;generate('Days', array('components' =&gt; 'Auth')); $Days-&gt;Auth-&gt;expects($this-&gt;once())-&gt;method('user')-&gt;will($this-&gt;returnValue(array(..userdata etc.))); $result = $this-&gt;testAction('/days/settings'); </code></pre> <p>It just keeps saying that the method was not called (altough the settings method inside the Days controller calls it exactly one time). What is wrong with the mockup? Hmmm..</p> <p>Any help would be appreciated!</p> <p>-edit- The code of the method that is being tested:</p> <pre><code>public function myDays() { $user = $this-&gt;Auth-&gt;user(); $days = $this-&gt;Day-&gt;find('all', array('conditions' =&gt; array('user_id' =&gt; $user['id'], 'active' =&gt; 1))); $this-&gt;set('days', $days); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload