我正在运行Laravel 5.4并使用Mail :: fake()和Mail :: assertSent()测试Mailables.对于诸如hasTo($ email)和hasCc($ email)之类的内容有断言,但似乎没有办法访问消息内容.我想测试一下,电子邮件正文包含一个特定的字符串.
伪代码:
Mail::assertSent(UserInvited::class, function($mail) use($token) {
    return $mail->bodyContains($token); # that method does not really exist
});
这可能吗?
eit*_*hed 11
遇到相同的情况,没有运气搜索 - 调试后:
Mail::assertSent(InstanceOfMailableEmail::class, function($mail) use ($needle) {
    // now your email has properties set
    $mail->build(); 
    //check the content body for a string
    return strpos($mail->viewData['content'], $needle) !== false;
});
您可以提取一个包来测试邮件。但截至目前,您必须发挥一点创意才能使其正常工作,因为他们尚未在需求中指定 Laravel 5.4.*。因此,进入您的composer.json文件并从此存储库构建您自己的包,并更新 Laravel 5.4.* 的要求,如下所示:
"repositories": [
    {
        "type": "package",
        "package": {
            "name": "spinen/laravel-mail-assertions",
            "version": "0.1.2",
            "require": {
                "php": ">=5.5.0",
                "illuminate/support": "~5.1.10|5.2.*|5.3.*|5.4.*",
                "swiftmailer/swiftmailer": "~5.1"
            },
            "require-dev": {
                "mockery/mockery": "^0.9.1",
                "phpunit/phpunit": "~4.0|~5.0",
                "psy/psysh": "^0.5.1",
                "satooshi/php-coveralls": "^0.6.1",
                "symfony/var-dumper": "~2.7|~3.0"
            },
            "autoload": {
                "psr-4": {
                    "Spinen\\MailAssertions\\": "src"
                }
            },
            "autoload-dev": {
                "psr-4": {
                    "Spinen\\MailAssertions\\": "tests"
                }
            },
            "config": {
                "preferred-install": "dist"
            },
            "dist": {
                "url": "https://github.com/spinen/laravel-mail-assertions/archive/0.1.1.zip",
                "type": "zip"
            }
        }
    }
],
然后,在 下require-dev添加您的自定义包:
"require-dev": {
    "spinen/laravel-mail-assertions": "^0.1.2"
},
安装后,MAIL_DRIVER=log在您的.env文件中进行设置并将该Spinen\MailAssertions\MailTracking特征引入您的测试中。此特征具有诸如seeEmailContains、seeEmailSubjectContains等方法来断言发送的任何电子邮件中都存在文本。
| 归档时间: | 
 | 
| 查看次数: | 1605 次 | 
| 最近记录: |