ユーザー新規登録はクリティカルなので、テストを書いておきたかった。
でも新規登録時にメールが送られて、ミドルウェアの設定をしないとそこでエラーになってしまう。
ローカル上では mailcatcher
動いているからいいけど、CI上ではそれをやりたくない。
なので Mock した。思ってたより簡単だった。
- Laravel 5.1
tests/integratin/UserSignupTest.php
<?php
namespace Tests\Integration;
use App\Models\User;
use Tests\TestCase;
class UserSignupTest extends TestCase
{
public function testUserSignup()
{
\Mail::ShouldReceive('send')
->once()
->andReturn(true);
$this->visit('/signup')
->type('hoge@example.com', 'email')
->type('secret', 'password')
->press('Sign up')
->seePageIs('/home');
$this->seeInDatabase('users', [
'email' => 'hoge@example.com'
]);
}
}
Mail::send() メソッドが実行されなければ、エラーが発生する。
できればメールの内容のチェックもしたい。
ShouldReceive('send')->with(...)
とか使えばいいんだろうか。