模拟CakePHP中的电子邮件组件调用非对象上的成员函数
我试图在模型中模拟电子邮件组件,但在第一个函数之后,Cake给了我一个错误:错误:对非对象调用成员函数to() 这是我的测试课:模拟CakePHP中的电子邮件组件调用非对象上的成员函数,cakephp,model,mocking,Cakephp,Model,Mocking,我试图在模型中模拟电子邮件组件,但在第一个函数之后,Cake给了我一个错误:错误:对非对象调用成员函数to() 这是我的测试课: <?php App::uses('User', 'Model'); class UserTest extends CakeTestCase { . . // I'm not copying the setUp and tearDown methods here, but they are there. . . public function testE
<?php
App::uses('User', 'Model');
class UserTest extends CakeTestCase {
.
.
// I'm not copying the setUp and tearDown methods here, but they are there.
.
.
public function testEmailTest(){
$CakeEmail = $this->getMock('CakeEmail',array('to','subject'));
$CakeEmail->expects($this->any())
->method('to')
->with($this->equalTo('ant@b.com'));
$CakeEmail->expects($this->once())
->method('subject')
->with($this->equalTo('hi'));
$this->User->CakeEmail = $CakeEmail;
$result = $this->User->emailCat();
}
}
蛋糕v2.3.1
也许有一种不同的方法可以在这个模拟函数上设置多个方法,但这是基于有人在网上发布的解决方案
(我知道我不应该从这里发送电子邮件,但对于这一注册功能,我想将其保留在这里,以便在电子邮件未发送时可以回滚交易)。您确定您有
返回$this代码>在函数的末尾,您应该仔细检查错误消息。它是在抱怨用户模型还是测试本身?这段代码破坏了MVC。
public $CakeEmail = null;
public function emailCat() {
if ( !$this->CakeEmail){
$this->CakeEmail = new CakeEmail();
}
$this->CakeEmail->subject('hi')
->to('ant@b.com')
->emailFormat('both')
->config('default')
->send('hi');
}