title: Use a different email config during unit testing in CakePHP
body: <p>I'm sending an email using the CakeEmail class in an action of one of my controllers. I have a unit test for this controller that was working fine before adding the email code. After adding the email I get this error:</p>
  
  <blockquote>
    <p>SocketException: Could not send email.</p>
  </blockquote>
  
  <p>This is down to the fact that I don't have any way of sending emails off my local machine.</p>
  
  <p>So I thought that maybe a good idea would be to have two different configuration options inside the EmailConfig class in Config/email.php (Similar to how the database config file works). The default using the Mail transport, and a test using the Debug transport. The issue with this is that, unlike the database config, Cake doesn't automatically switch between the two during testing.</p>
  
  <p>The only thing I have thought of is to add a constructor to the EmailConfig class and test if we're unit testing, but I'm not sure what the check should be.</p>
  
  <p>Something along the lines of this:</p>
  
  <pre><code>class EmailConfig {
  
      public $default = array(
          'transport' =&gt; 'Mail'
      );
  
      public $test = array(
          'transport' =&gt; 'Debug'
      );
  
      public function __construct() {
          if ($isUnitTesting) {
              $this-&gt;default = $this-&gt;test;
          }
      }
  
  }
  </code></pre>
  
  <p>Would my way suggested above be a good idea? If not, what other ways can I use a different transport for the email during unit testing?</p>
  
  <hr>
  
  <p><strong>Update - 4/10/2012</strong></p>
  
  <p>I think I was going about this the wrong way. Looking at <a href="https://stackoverflow.com/a/8600556/343465">this answer</a> it seems that even the <code>$default</code> config isn't loaded by default, you have to specify it by calling the <code>CakeEmail::config()</code> method or give it in the constructor. So I think this leaves me two options now:</p>
  
  <ol>
  <li>In the controller check if we're unit testing (somehow?) and then use the 'test' config.</li>
  <li>Set up my computer to be able to send emails.</li>
  </ol>
  
  <p>I'd rather do the first but unsure how this can be done without bloating the controller action with checks if we're unit testing, it seems wrong to do this.</p>