如何在Laravel Mailable上进行断言 [英] How to make assertions on a Laravel Mailable

查看:112
本文介绍了如何在Laravel Mailable上进行断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试中,我想使用Mail::assertSent()在Mailable上做出一些断言,如下所示:

In a test I would like to make some assertions on a Mailable using Mail::assertSent(), like this:

Mail::assertSent(MyMailable::class, function ($mail) use ($user) {
    return $mail->hasTo($user->email);
});

到目前为止,我发现hasTo()hasFrom()hasBcc()和所有其他has*()方法都非常有效.但是,当断言Mailable上的特定属性存在时,例如subject,该属性显示为null,断言失败:

So far, I have found the hasTo(), hasFrom(), hasBcc() and all the other has*() methods work just great. However, when asserting a particular attribute on the Mailable exists, for example subject the attribute shows up as null and the assertion fails:

Mail::assertSent(MyMailable::class, function ($mail) {
    return $mail->subject === 'My Subject';
});

我认为这是因为我已经在build()方法中配置了所有Mailable属性,在断言阶段可能尚未调用该属性,因此尚未在对象上设置这些属性.

I believe this is because I have configured all of the Mailable attributes within the build() method which at the stage of the assertion probably hasn't been invoked, so the attributes are not set on the object yet.

我认为使用build()方法是基于文档的正确方法:

I thought using the build() method was the correct approach to take based on the docs:

所有可邮寄类的配置均在build方法中完成.在此方法中,您可以调用从,主题,查看和附加之类的各种方法来配置电子邮件的演示和传递.

All of a mailable class' configuration is done in the build method. Within this method, you may call various methods such as from, subject, view, and attach to configure the email's presentation and delivery.

https://laravel.com/docs/5.5/mail#writing-mailables

我发现当我改为在构造函数上设置属性时,我可以得到有关Mailable属性的断言:

I have found that I can get assertions on the Mailable's attributes working when I instead set the attributes on the constructor:

class MyMail extends Mailable
{
    public function __construct()
    {
        $this->subject = 'My Subject';
    }

    public function build() {
        return $this->subject('My Subject')->view('emails/my-email')
    }
}

但是,我觉得这种方法是错误的,因为我感觉自己正在更改代码以适合我的测试.

However, I feel this approach is wrong because I feel like I am changing my code to suit my tests.

因此,我想知道是否有更好的方法针对Mailable的属性进行断言?任何帮助将不胜感激,谢谢!

So, I would like to know if there is a better approach to making assertions against attributes on a Mailable? Any help would be most appreciated, thank you!

编辑1

测试类(删除了不相关的代码)

Test class (irrelevant code stripped out)

/** @test */
function a_notification_is_sent_when_an_application_is_updated()
{
    Mail::fake([RequiresVerification::class]);

    // some set up and factory methods called here...

    // the listener for this event sends mail
    ApplicationUpdated::dispatch($application);

    // this assertion passes
    Mail::assertSent(RequiresVerification::class);

    // this assertion does not pass when subject is set on the build()  
    // method but passes when subject is set on the constructor
    Mail::assertSent(RequiresVerification::class, function ($mail) use ($user) {
        return $mail->subject === 'hello';
    });
}

编辑2

我目前正在查看 <所有has*方法都使用的c8>方法,以查看它如何处理针对我认为是Mailable属性(收件人,发件人,密件抄送,抄送等)的断言.也许可以使用类似的方法将Mailable对象扩展为添加新的属性声明?

I am currently looking at the hasRecipient() method, which all has* methods use, to see how it handles making assertions against what I assume are Mailable attributes (to, from, bcc, cc, etc). Perhaps the Mailable object can be extended to add new attribute assertions using a similar approach?

https://github.com /laravel/framework/blob/5.5/src/Illuminate/Mail/Mailable.php#L540

推荐答案

您可以通过在assertSent闭包内调用build()方法,然后对build()方法中配置的属性进行声明:

You can make assertions against attributes configured in the build() method by calling the build() method within the assertSent closure before making the assertions:

Mail::assertSent(MyMailable::class, function ($mail) {
    $mail->build();
    return $mail->subject === 'My Subject';
});

感谢laohcasts上的@ohffs对此提供帮助: https://laracasts.com/discuss/channels/testing/how-to-make-assertions-on-a-laravel-mailable

Thanks to @ohffs on laracasts for helping with this: https://laracasts.com/discuss/channels/testing/how-to-make-assertions-on-a-laravel-mailable

这篇关于如何在Laravel Mailable上进行断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆