Laravel unit testing emails

前端 未结 8 1515
难免孤独
难免孤独 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();
    

提交回复
热议问题