colombo / autohttptests
自动为 Laravel 生成 http 测试
v1.0.5
2020-08-06 09:50 UTC
Requires
- php: ~5.6|~7.0
- illuminate/support: ^5.5|^5.6|^6.0
Requires (Dev)
- phpunit/phpunit: >=5.4.3
- squizlabs/php_codesniffer: ^2.3
README
不再手动编写测试 =D
只需执行命令,记录您的操作作为 http 测试
php artisan autohttptest:create
该命令将 拦截 您的请求,并将响应转换为测试。
完成后,您的测试将保存在 tests/Feature/
视频演示
它测试了什么?
- 以相同用户身份执行请求
- 使用相同的动词(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)。请参阅 许可文件 以获取更多信息。