我在做注册用户
public function register(RegistrationUser $request)
{
$user = $this->usersRepo->create($request->all());
$user->activation_token = str_random(48);
$user->save();
Mail::to($user->email)->queue(new ActivationAccount($user->first_name, $user->last_name, $user->email, $request->input('password'), $url));
return redirect()->route('successful.registration')
}
我的注册测试是:
public function it_creates_a_new_user()
{
$this->withExceptionHandling();
$response = $this->get(route('register'))
->assertStatus(200);
$this->post('register', [
'first_name' => 'Juan',
'last_name' => 'Lopez',
'email' => 'jlopez@gmail.com',
'password' => 'secret',
'activation_tone' => str_random(48)
])->assertRedirect(route('successful.registration'));
$this->assertDatabaseHas('users', [
'email' => 'jlopez@gmail.com',
]);
}
我有两个问题:
1) 如何编写测试来发送注册电子邮件并验证它是否成功发送和到达?
2) 当用户点击他的电子邮件时,他会调用一个方法,在该方法中传递激活 token 以激活他的帐户
最佳答案
在我看来你应该使用 mail fake ,这将阻止邮件被发送。然后您可以断言邮件已发送给用户,甚至可以检查他们收到的数据。
请阅读 laravel 文档:https://laravel.com/docs/5.6/mocking#mail-fake
必须有一个处理激活 token 和功能的路由,所以尝试获取 token 并使用特定 token 调用路由
注意:作为开发人员,我们需要确保我们的代码正常工作,我们的测试正在确认这一点,发送和传递电子邮件不应包含在他们认为按预期工作(由任何电子邮件服务提供商提供)的范围内。
关于testing - 使用 TDD Laravel 5.6 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50120224/