Laravel unit testing emails

前端 未结 8 1511
难免孤独
难免孤独 2021-02-20 10:12

My system sends a couple of important emails. What is the best way to unit test that?

I see you can put it in pretend mode and it goes in the log. Is there something t

相关标签:
8条回答
  • 2021-02-20 10:58

    "Option 1" from "@The Shift Exchange" is not working in Laravel 5.1, so here is modified version using Proxied Partial Mock:

    $mock = \Mockery::mock($this->app['mailer']->getSwiftMailer());
    $this->app['mailer']->setSwiftMailer($mock);
    $mock
        ->shouldReceive('send')
        ->withArgs([\Mockery::on(function($message)
        {
            $this->assertEquals('My subject', $message->getSubject());
            $this->assertSame(['foo@bar.com' => null], $message->getTo());
            $this->assertContains('Some string', $message->getBody());
            return true;
        }), \Mockery::any()])
        ->once();
    
    0 讨论(0)
  • 2021-02-20 11:02

    I think that inspecting the log is not the good way to go.

    You may want to take a look at how you can mock the Mail facade and check that it receives a call with some parameters.

    0 讨论(0)
提交回复
热议问题