testing - 使用 TDD Laravel 5.6 发送电子邮件

标签 testing laravel-5 phpunit tdd

我在做注册用户

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 以激活他的帐户

最佳答案

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

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

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

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

关于testing - 使用 TDD Laravel 5.6 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50120224/

相关文章:

ios - UI 自动化命令行配置通用应用程序的模拟器

php - 拉维尔 5 : allow user to edit post if he's the owner of the post or the owner of the forum category

python - 在 python 模块中测试示例代码

c++ - pImpl 习语和可测试性

javascript - 带有 Protractor 的 Jasmine 自定义匹配器,用于检查浏览器标题

laravel - 如何在 Laravel 中在 MIN 和 MAX 之间搜索

php - SSH 到动态远程服务器并运行命令 - Laravel 5.8

php - PHPUnit 和 Jenkins 中的 "//proc/tty/driver"权限错误?

rest - 使用 phpunit 和doctrine 测试 symfony Restful api

当我的测试类使用自己的构造方法时,PHPUnit 6.1.x 抛出 array_merge() 错误