基础
优先级高到低
- phpunit.xml APP_ENV = testing
- .env.testing
测试项目
- 测试哪些?
- 数据库测试
- 测试表单验证
- 测试文件上传
- 测试邮件发送
- 测试远程 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());