colombo/autohttptests

自动为 Laravel 生成 http 测试

v1.0.5 2020-08-06 09:50 UTC

This package is auto-updated.

Last update: 2024-09-06 19:23:24 UTC


README

Latest Version on Packagist Software License Total Downloads

不再手动编写测试 =D

只需执行命令,记录您的操作作为 http 测试

php artisan autohttptest:create

imagen

该命令将 拦截 您的请求,并将响应转换为测试。

完成后,您的测试将保存在 tests/Feature/

视频演示

autohttptests-gif

它测试了什么?

  • 以相同用户身份执行请求
  • 使用相同的动词(GET、PUT、POST)和相同的参数发出请求
  • 断言 http 响应代码
  • 断言错误
  • 断言重定向

示例代码

<?php

namespace Tests\Feature;
use Tests\TestCase;

class SomethingTest extends TestCase
{
    public function testAutoHttpTest()
    {
        $this
        ->actingAs(\App\Models\User::find(1))
        ->post('home/something', [
            'name'         => 'a',
            'lastname'     => 'a',
            'city'         => '',
            'hobbies'   => '',
            'twitter_username' => 'a',
        ])
        ->assertStatus(302)
        ->assertSessionHasErrors([
            'name',
            'country_id',
            'twitter_username',
        ]);

        $this
        ->actingAs(\App\Models\User::find(1))
        ->post('home/something', [
            'name'              => 'asdfa',
            'lastname'          => 'asdfa',
            'country_id' => '1',
            'city'              => '',
            'hobbies'        => '',
            'twitter_username'      => 'asdfa',
        ])
            ->assertStatus(302)
            ->assertRedirect('home/something');
    }
}

注意

这里我们捕获了一个不成功的 post 请求,并带有错误。然后,一个 成功的 post 请求带有重定向

安装

通过 Composer

$ composer require eduardoarandah/autohttptests

如果您使用的是 Laravel < 5.4,请在 config/app.php 中添加到 providers

EduardoArandaH\AutoHttpTests\AutoHttpTestsServiceProvider::class,

用法

php artisan autohttptest:create

致谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 以获取更多信息。