TDD

来自技术笔记
跳到导航 跳到搜索

phpunit.xml 文件定义了 APP_ENV 为 testing

然后,我们新建了 .env.testing,所以会从这个文件读取配置,然后再追加 phpunit.xml 覆盖 env

测试哪些?[编辑 | 编辑源代码]

  • 数据库测试
  • 测试表单验证
  • 测试文件上传
  • 测试邮件发送
  • 测试远程 API 调用
  • 测试「异常」抛出
  • 模拟登录用户
  • contract 测试
  • 测试命令行
  • Dusk 测试 (浏览器测试,模拟用户行为,包含 JS 测试)

覆盖率[编辑 | 编辑源代码]

--coverage-html .

辅助方法[编辑 | 编辑源代码]

抛出异常,如 500。注:现在高版本已经可以看到错误信息了。加如下代码和不加,返回内容会有些不一样。

$this->withoutExceptionHandling();

HTTP 响应

$this->withExceptionHandling()

state[编辑 | 编辑源代码]

public function suspended()
{
    return $this->state([
        'account_status' => 'suspended',
    ]);
}

数据库迁移[编辑 | 编辑源代码]

use RefreshDatabase

setUp[编辑 | 编辑源代码]

protected function setUp(): void
    {
        parent::setUp();

        $this->withoutExceptionHandling();
    }

登录[编辑 | 编辑源代码]

$this->actingAs($user = factory(User::class)->create());

ORM[编辑 | 编辑源代码]

$this->assertInstanceOf('Illuminate\Database\Eloquent\Relations\BelongsTo', $post->user());