$ loading_
帮助开发者系统掌握 Laravel 测试驱动开发与自动化测试实践。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "laravel-tdd" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/laravel-tdd/SKILL.md 2. 保存为 ~/.claude/skills/laravel-tdd/SKILL.md 3. 装好后重载技能,告诉我可以用了
请用 Pest 为 Laravel 用户注册接口编写测试,覆盖成功注册、邮箱重复、密码不符合规则三种情况,并展示对应的工厂与断言写法。
得到一组可运行的 Pest 接口测试代码,包含工厂准备、请求示例和关键断言。
请演示如何在 Laravel 中测试使用 Sanctum 保护的 API,包含已认证用户访问成功、未认证返回 401,以及如何在测试中模拟登录。
得到 Sanctum 认证测试示例,说明认证上下文构造方式与接口断言方法。
我有一个 Laravel 服务类依赖外部支付网关,请用 PHPUnit 说明如何通过 mocking 编写单元测试,并补充覆盖成功、异常和重试逻辑的测试案例。
得到基于 PHPUnit 的单元测试方案与示例代码,展示 mock、依赖隔离和覆盖率思路。
Test-driven development for Laravel applications using PHPUnit, Pest, Laravel factories, and testing helpers.
// Step 1: RED — Write a failing test
public function test_a_product_can_be_created(): void
{
$product = Product::factory()->create(['name' => 'Test Product']);
$this->assertDatabaseHas('products', ['name' => 'Test Product']);
}
// Step 2: GREEN — Write the migration, model, and factory
// Step 3: REFACTOR — Improve while keeping tests green
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
</php>
</phpunit>
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
protected function setUp(): void
{
parent::setUp();
// Call $this->withoutExceptionHandling() only in tests that
// test non-HTTP exceptions; it suppresses assertStatus() etc.
}
// Helper: Authenticate and return user
protected function actingAsUser(): mixed
{
$user = \App\Models\User::factory()->create();
$this->actingAs($user);
return $user;
}
protected function actingAsAdmin(): mixed
{
$admin = \App\Models\User::factory()->admin()->create();
$this->actingAs($admin);
return $admin;
}
}
// database/factories/UserFactory.php
class UserFactory extends Factory
{
protected static ?string $password = null;
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'role' => 'user',
];
}
public function admin(): static
{
return $this->state(fn (array $attributes) => ['role' => 'admin']);
}
public function unverified(): static
{
return $this->state(fn (array $attributes) => ['email_verified_at' => null]);
}
}
// database/factories/ProductFactory.php
class ProductFactory extends Factory
{
public function definition(): array
{
return [
'name' => fake()->unique()->words(3, true),
'slug' => fn (array $attrs) => Str::slug($attrs['name']),
'description' => fake()->paragraph(),
'price' => fake()->numberBetween(100, 100000),
'stock' => fake()->numberBetween(0, 100),
'is_active' => true,
…
为 Quarkus 项目执行发布前验证闭环,涵盖构建、测试、扫描与差异审查。
帮助用户用 pytest 与 TDD 建立高质量 Python 测试体系。