title: How to test CakeEmail is called from a model
body: <p>In CakePHP 2.1 I'm trying to test that the CakeEmail::to() method is called from my model test case with the correct "to" email (In this example: cat@gmail.com).</p>
  
  <p>I want the following test to pass but I get: </p>
  
  <pre><code>Expectation failed for method name is equal to &lt;string:to&gt; when invoked 1 time(s).
  Method was expected to be called 1 times, actually called 0 times.
  </code></pre>
  
  <p>Here's the code in the Model and the test case:</p>
  
  <pre><code>&lt;?php
  
  // Model/Job.php
  
  App::uses('AppModel', 'Model');
  App::uses('CakeEmail', 'Network/Email');
  
  class Job extends AppModel {
  
          public function emailCat() {
                  $CakeEmail = new CakeEmail();
                  $CakeEmail-&gt;to('cat@gmail.com');
                  $CakeEmail-&gt;subject('hello!');
                  $CakeEmail-&gt;emailFormat('text');
                  $CakeEmail-&gt;config('default');
                  $CakeEmail-&gt;send('hi');
          }
  
  }
  
  // Test/Model/JobTest.php
  
  class JobTestCase extends CakeTestCase {
  
          public function setUp() {
                  parent::setUp();
                  $this-&gt;Job = ClassRegistry::init('Job');
          }      
  
          public function testEmailCat() {
  
                  // I want to assert CakeEmail::to() is called with correct email
                  $CakeEmail = $this-&gt;getMock('CakeEmail' , array('to'));
                  $CakeEmail-&gt;expects($this-&gt;once())
                      -&gt;method('to')
                      -&gt;with($this-&gt;equalTo('cat@gmail.com'));            
  
                  $result = $this-&gt;Job-&gt;emailCat();
          }
  
  }
  </code></pre>