title: cakephp testing controller: database mock doesn't return anything
body: <p>I'm new to CakePHP and PHPUnit and I want to test one of my controllers. So far I have this method I like to test:</p>
  
  <pre><code>public function getAlternatives($id = null){
      // ID parameter given or is NaN?
      if (!$id || !is_numeric($id)) {
          throw new BadRequestException();
      }
  
      // Error if object not found
      if (!$this-&gt;Designitem-&gt;exists($id)) {
          throw new NotFoundException();
      }
  
      $alternatives = $this-&gt;Designitem-&gt;getItemsWithFunritures($id);
  
      $this-&gt;set(array(
          'alternatives' =&gt; $alternatives,
          '_serialize' =&gt; array('alternatives'),
          '_jsonp' =&gt; true));
  }
  </code></pre>
  
  <p>Now I want to test whether the getItemsWithFurnitures is called on my model Designitem. To test this I implemented this test:</p>
  
  <pre><code>public function testGetAlternativesWithValidID() {
      $id = 1;
      $url = array('controller' =&gt; 'Designitems', 'action' =&gt; 'getAlternatives', $id . '.json');
  
      $expected = array(
          'Designitem' =&gt;
              array(
                  'id' =&gt; 1,
                  'design_id' =&gt; 1,
                  'furniture_id' =&gt; 1,
                  'template_id' =&gt; 1,
              ),
      );
  
      $designitems = $this-&gt;generate('Designitems', array(
          'methods' =&gt; array(
              'getAlternatives',
          ),
          'models' =&gt; array(
              'Designitem' =&gt; array('getItemsWithFunritures'),
          ),
      ));
      $designitems-&gt;Designitem
          -&gt;expects($this-&gt;once())
          -&gt;method('getItemsWithFunritures')
          -&gt;will($this-&gt;returnValue($expected));
  
      $result = $this-&gt;testAction($url);
  
      $this-&gt;assertEquals(json_encode($expected), $result);
  }
  </code></pre>
  
  <p>EDIT: My Designitems Fixture contains one stubbed entry. It looks like this:</p>
  
  <pre><code>class DesignitemFixture extends CakeTestFixture {
  
  public $fields = array(
      'id' =&gt; array('type' =&gt; 'integer', 'null' =&gt; false, 'default' =&gt; null, 'unsigned' =&gt; false, 'key' =&gt; 'primary'),
      'design_id' =&gt; array('type' =&gt; 'integer', 'null' =&gt; false, 'default' =&gt; null, 'unsigned' =&gt; false),
      'furniture_id' =&gt; array('type' =&gt; 'integer', 'null' =&gt; false, 'default' =&gt; null, 'unsigned' =&gt; false),
      'template_id' =&gt; array('type' =&gt; 'integer', 'null' =&gt; true, 'default' =&gt; null, 'unsigned' =&gt; false),
      'templateitem_id' =&gt; array('type' =&gt; 'integer', 'null' =&gt; true, 'default' =&gt; null, 'unsigned' =&gt; false),
      'amount' =&gt; array('type' =&gt; 'integer', 'null' =&gt; false, 'default' =&gt; null, 'length' =&gt; 5, 'unsigned' =&gt; false),
      'feedback' =&gt; array('type' =&gt; 'string', 'null' =&gt; true, 'default' =&gt; null, 'length' =&gt; 20, 'collate' =&gt; 'utf8_general_ci', 'charset' =&gt; 'utf8'),
      'clicks' =&gt; array('type' =&gt; 'integer', 'null' =&gt; true, 'default' =&gt; '0', 'unsigned' =&gt; false),
      'ilike' =&gt; array('type' =&gt; 'boolean', 'null' =&gt; false, 'default' =&gt; null),
      'dislike' =&gt; array('type' =&gt; 'boolean', 'null' =&gt; false, 'default' =&gt; null),
      'dislike_reason' =&gt; array('type' =&gt; 'string', 'null' =&gt; false, 'default' =&gt; null, 'length' =&gt; 100, 'collate' =&gt; 'utf8_general_ci', 'charset' =&gt; 'utf8'),
      'dislike_comment' =&gt; array('type' =&gt; 'string', 'null' =&gt; false, 'default' =&gt; null, 'length' =&gt; 2000, 'collate' =&gt; 'utf8_general_ci', 'charset' =&gt; 'utf8'),
      'parent_id' =&gt; array('type' =&gt; 'integer', 'null' =&gt; true, 'default' =&gt; null, 'unsigned' =&gt; false),
      'has_alternatives' =&gt; array('type' =&gt; 'boolean', 'null' =&gt; true, 'default' =&gt; null),
      'is_selected' =&gt; array('type' =&gt; 'boolean', 'null' =&gt; true, 'default' =&gt; null),
      'is_recommendation' =&gt; array('type' =&gt; 'boolean', 'null' =&gt; true, 'default' =&gt; null),
      'deleted' =&gt; array('type' =&gt; 'integer', 'null' =&gt; false, 'default' =&gt; null, 'length' =&gt; 4, 'unsigned' =&gt; false),
      'deleted_date' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; true, 'default' =&gt; null),
      'created' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; true, 'default' =&gt; null),
      'modified' =&gt; array('type' =&gt; 'datetime', 'null' =&gt; true, 'default' =&gt; null),
      'indexes' =&gt; array(
          'PRIMARY' =&gt; array('column' =&gt; 'id', 'unique' =&gt; 1)
      ),
      'tableParameters' =&gt; array('charset' =&gt; 'utf8', 'collate' =&gt; 'utf8_general_ci', 'engine' =&gt; 'InnoDB')
  );
  
  public $records = array(
      array(
          'id' =&gt; 1,
          'design_id' =&gt; 1,
          'furniture_id' =&gt; 1,
          'template_id' =&gt; 1,
          'templateitem_id' =&gt; 1,
          'amount' =&gt; 1,
          'feedback' =&gt; 'Lorem ipsum dolor ',
          'clicks' =&gt; 1,
          'ilike' =&gt; 1,
          'dislike' =&gt; 1,
          'dislike_reason' =&gt; 'Lorem ipsum dolor sit amet',
          'dislike_comment' =&gt; 'Lorem ipsum dolor sit amet',
          'parent_id' =&gt; 1,
          'has_alternatives' =&gt; 1,
          'is_selected' =&gt; 1,
          'is_recommendation' =&gt; 1,
          'deleted' =&gt; 0,
          'deleted_date' =&gt; '2016-07-14 12:05:39',
          'created' =&gt; '2016-07-14 12:05:39',
          'modified' =&gt; '2016-07-14 12:05:39'
      ),
  );
  </code></pre>
  
  <p>}</p>
  
  <p>When I run the test, I get an error that the getItemsWithFurnitures returned null: </p>
  
  <p><a href="http://i.stack.imgur.com/N1ID0.png" rel="nofollow">Test result</a></p>
  
  <p>Can anyone tell me what I'm doing wrong? Thank you!</p>