php - CakePHP 2 testing model with no table using mock method for email -
i'm attempting write test model has no table sends email if data passes validation in cakephp 2.
to test want assert data passes validation , therefore send email without sending one. attempting create mock method cakeemail. however, test failing because $usedbconfig hasn't defined mock method:-
undefined property: mock_mymodel_7a1fb8d0::$usedbconfig   i assume issue model not having table, cannot see how resolve it.
my model looks (excluding validation rules):-
<?php  app::uses('cakeemail', 'network/email');  class mymodel extends appmodel {      public $usetable = false;      public function send($data) {          $this->set($data);          if ($this->validates() === false) {             return false;         } else {             $email = $this->getemailer();             $email->from($data['mymodel']['email_from']);             $email->to($data['mymodel']['email_to']);             $email->subject($data['mymodel']['subject']);             $email->send($data['mymodel']['message']);         }          return true;      }      public function getemailer() {         return new cakeemail();     }  }   my test is:-
<?php class mymodel extends caketestcase {      public function setup() {         parent::setup();         $this->mymodel = classregistry::init('mymodel');     }      public function testsend() {          $emailer = $this->getmock(             'cakeemail',             array(                 'to',                 'emailformat',                 'subject',                 'replyto',                 'from',                 'template',                 'viewvars',                 'send'             )         );         $emailer->expects($this->any())->method('send')->will($this->returnvalue(true));          $mymodel = $this->getmockformodel('mymodel', array('getemailer'));         $mymodel->expects($this->once())->method('getemailer')->will($this->returnvalue($emailer));          $data = array(             'mymodel' => array(                 'email_to' => 'foo@example.com',                 'email_from' => 'bar@example.com',                 'subject' => 'foo bar',                 'message' => ''             )         );          $result = $mymodel->send($data);         $this->asserttrue($result);      }  }   any appreciated. first time i've tried/needed mock method in cake using tests.
class name should have been mymodeltest rather mymodel. cakephp's naming convention needs adhered to.
Comments
Post a Comment