使用TDD Laravel 5.6发送电子邮件

问题描述 投票:1回答:1

我在做注册用户

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' => '[email protected]',
        'password' => 'secret',
        'activation_tone' => str_random(48)
    ])->assertRedirect(route('successful.registration'));

    $this->assertDatabaseHas('users', [
        'email' => '[email protected]',
    ]);

  }

我有两个问题:

1)如何编写测试以发送注册电子邮件并验证其是否发送和到达?

2)当用户点击他的电子邮件时,他调用一种方法,其中传递激活令牌以激活他的帐户

testing laravel-5 phpunit tdd
1个回答
1
投票
  1. 在我看来,你应该使用邮件假,这将阻止邮件被发送。然后,您可以断言可邮寄给用户甚至检查他们收到的数据。 请阅读laravel docs:https://laravel.com/docs/5.6/mocking#mail-fake
  2. 必须有一条处理激活令牌和功能的路由,因此尝试使用特定令牌获取令牌和呼叫路由

注意:作为开发人员,我们需要确保我们的代码适用于我们的测试确认,发送和发送电子邮件不应被涵盖,因为他们认为按预期工作(由任何电子邮件服务提供商)。

© www.soinside.com 2019 - 2024. All rights reserved.