如何在不發送電子郵件的情況下測試此代碼段?Laravel單元測試如何使用電子郵件測試事件而不發送電子郵件
public function forgot()
{
$isValid = $this->updateForm->valid(Input::only('email'));
if ($isValid) {
$result = $this->user->forgot($this->updateForm->data());
if (isset($result['user']) && ($result['success'] > 0)) {
Event::fire('user.mail.forgot', array('data'=>$result['user']));
return Response::json(array('success'=>1),200);
}
$error = isset($result['error'])?array_pop($result):trans('user.generror');
return Response::json(array(
'success'=>0,
'errors' => array('error'=>array($error))),
200);
}
return Response::json(array(
'success' => 0,
'errors' => $this->updateForm->errors()),
200
);
}
現在我測試:
public function _getSuccess($content)
{
$json = str_replace(")]}',\n", '', $content);
$native = json_decode($json);
return $native->success;
}
public function _set200($method, $uri, $parameters = array())
{
$this->client->setServerParameter('HTTP_X-Requested-With', 'XMLHttpRequest');
$response = $this->call($method, $uri, $parameters);
$this->assertResponseStatus(200);
return $response;
}
public function testUserForgot200Success()
{
$response = $this->_set200('POST', '/api/v1/users/forgot', array('email' => $this->userEmail));
$this->assertSame(1, $this->_getSuccess($response->getContent()));
}
但這種方式我必須設置郵件配置文件在測試文件夾和sustem發送電子郵件:(