我试图使用get() / posts()类提供的posts()/ IntegrationTestCase方法为发送电子邮件的操作编写测试。
代码是这样的:
$this->getMailer('User')
->set('someVarName', 'someVarValue)
->send('forgotPassword', [$user]); 正常情况下,这段代码可以工作。
但是通过测试,我得到了这个错误:
1) MeCms\Test\TestCase\Controller\UsersControllerTest::testForgotPassword
BadMethodCallException: Cannot send email, transport was not defined. Did you call transport() or define a transport in the set profile?
/home/mirko/Libs/Plugins/MeCms/vendor/cakephp/cakephp/src/Mailer/Email.php:2049
/home/mirko/Libs/Plugins/MeCms/vendor/cakephp/cakephp/src/Mailer/Mailer.php:252
/home/mirko/Libs/Plugins/MeCms/src/Controller/UsersController.php:213
/home/mirko/Libs/Plugins/MeCms/vendor/cakephp/cakephp/src/Controller/Controller.php:440
/home/mirko/Libs/Plugins/MeCms/vendor/cakephp/cakephp/src/Http/ActionDispatcher.php:119
/home/mirko/Libs/Plugins/MeCms/vendor/cakephp/cakephp/src/Http/ActionDispatcher.php:93
/home/mirko/Libs/Plugins/MeCms/vendor/cakephp/cakephp/src/Routing/Dispatcher.php:60
/home/mirko/Libs/Plugins/MeCms/vendor/cakephp/cakephp/src/TestSuite/LegacyRequestDispatcher.php:61
/home/mirko/Libs/Plugins/MeCms/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:426
/home/mirko/Libs/Plugins/MeCms/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:360
/home/mirko/Libs/Plugins/MeCms/tests/TestCase/Controller/UsersControllerTest.php:345我一直在寻找一些,但我不知道如何设置一个传输仅为测试。
谢谢。
发布于 2017-05-03 11:42:04
我没有遇到这样的要求,但下面的要求应该是可行的。
在您的/tests/bootstrap.php中定义一个常量,这样我们就可以判断我们是否处于测试环境中:
define('_TEST', true);
// important: define above requiring the /config/bootstrap.php
require dirname(__DIR__) . '/config/bootstrap.php';在/config/bootstrap.php中,在加载默认app配置文件后,对照常量进行检查:
Configure::load('app', 'default', false);
// load an additional config file `/config/app_testing.php` in testing environment
if (defined('_TEST') && _TEST === true) {
Configure::load('app_tests');
}最后,创建用于测试和覆盖某些默认配置值的配置文件/config/app_tests.php:
<?php
return [
'Email' => [
'default' => [
'transport' => 'gmail',
'log' => true
]
],
'EmailTransport' => [
'gmail' => [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'GoogleMailUserName',
'password' => 'GoogleMailPassword',
'className' => 'Smtp'
]
]
];https://stackoverflow.com/questions/43757618
复制相似问题