【问题标题】:Send email with TDD Laravel 5.6使用 TDD Laravel 5.6 发送电子邮件
【发布时间】:2018-10-11 17:05:50
【问题描述】:

我在做注册用户

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) 当用户点击他的电子邮件时,他调用一个传递激活令牌的方法来激活他的帐户

【问题讨论】:

    标签: testing laravel-5 phpunit tdd


    【解决方案1】:
    1. 在我看来你应该使用 mail fake ,这将阻止邮件被发送。然后,您可以断言邮件已发送给用户,甚至可以检查他们收到的数据。

      请阅读 laravel 文档:https://laravel.com/docs/5.6/mocking#mail-fake

    2. 必须有一个路由处理激活令牌和功能,所以尝试获取令牌并使用特定令牌调用路由

    注意:作为开发人员,我们需要确保我们的代码可以正常工作,我们的测试正在确认,发送和传递电子邮件不应被包括在内,因为它们被认为可以按预期工作(由任何电子邮件服务提供商)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-28
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 2018-02-01
      • 1970-01-01
      相关资源
      最近更新 更多