3

get()我正在尝试使用类提供的/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

我一直在寻找,但我不明白如何设置仅用于测试的传输。

谢谢。

4

1 回答 1

1

我没有遇到这样的要求,但是以下应该可以工作。

/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'
        ]
    ]
];
于 2017-05-03T11:42:04.110 回答