0

我有这封电子邮件

Mail::send([], [], function ($message) use ($email) {
    $message->to($email->to)
        ->subject($email->subject)
        ->setBody($email->content, 'text/html');
});

如何断言此邮件是在测试中发送的?

使用Mail::fake()外观,但Mail::assertSent()需要一个字符串 Mailable。

欢迎任何建议。

4

3 回答 3

1

默认情况下,PHPUnit 使用array邮件驱动程序并将所有发送的邮件存储在一个数组中。

因此,一种选择是放弃Mail::fake(),在测试中触发电子邮件,然后检查消息数组。

我在测试中使用的自定义断言:

protected function assertMailSentTo($user, $times = 1)
{
    // resolves the mail driver and gets messages
    $messages = app('swift.transport')->messages();

    $filtered = $messages->filter(function ($message) use ($user) {
        return array_key_exists($user->email, $message->getTo());
    });

    $expected = $times;

    $actual = $filtered->count();

    $this->assertTrue(
        $expected === $actual,
        "Sent {$actual} messages instead of {$expected}."
    );
}
于 2019-11-14T11:54:50.470 回答
0

Mail::send 返回一个值。尝试将其分配给变量并检查值;应该为 true(如果邮件已发送)或 false(如果未发送)。此外,您可以使用像 Mailinator 这样的网站来创建一个临时 ID 来接收邮件。

于 2019-11-14T11:40:36.977 回答
0

几个月前我问过同样的问题,我决定测试是否收到了一条消息。

也许它违反了“不要嘲笑你不拥有的东西”的规则,但因为它对我有用,所以我决定保留它。

我不知道您是否像我一样使用Behat ,但我希望它可以帮助某人。我也在这里使用MailTrap API。这是值得的。

好吧,一旦你使用 Behat,在你FeatureContext.php创建一个这样的函数:

/**
 * @Given I received a password reset token link at inbox
 */
public function iReceivedAPasswordResetTokenLinkAtInbox()
{
    $inbox = env('MAILTRAP_INBOX');
    $mailTrapToken = env('MAILTRAP_TOKEN');

    $options = [
        'headers' => [
            'Api-Token' => $mailTrapToken              
        ]            
    ];

    $client = new GuzzleHttp\Client($options);
    $apiMailTrap = 'https://mailtrap.io/api/v1';

    $response = $client->request('GET', "{$apiMailTrap}/inboxes/{$inbox}/messages");

    $msg = json_decode($response->getBody()->getContents());

    $response = $client->request('GET', "{$apiMailTrap}/inboxes/{$inbox}/".
        "messages/{$msg[0]->id}/body.txt");

    // Here you can get the message body and make any verification you need
    $bodyTxt = $response->getBody()->getContents();

    // In my case, I had to check if a URL was successfully sent, so        
    $this->tokenPasswordReset = trim(substr($bodyTxt, strpos($bodyTxt,'/resetpassword/') + 15, 60));         

    print('token: '.$this->tokenPasswordReset);

    // Then I made my assertion
    PHPUnit::assertNotEmpty($this->tokenPasswordReset);
}

在我的例子中,一旦我有了上面的函数,为了测试一个端点,我简单地添加了一个名为resetpw.feature包含的文件:

Feature: Changing Password
In order to reset an user password
As an user
I want to reset my password and update it

@password
Scenario: Users want to reset their passwords
Given I send a POST request to "/resetpassword" with parameters:
| key        | value                    |
| email      | behat@mytests.dev        |
Then the response status code should be 200

Given I received a password reset token link at inbox
# And here comes the previously created function!

When I access a password reset link with this token
Then the response status code should be 200

然后我跑去behat --tags @password测试。

而已。我希望它可以激励某人!

于 2019-11-14T11:46:20.093 回答