laravel-testing by leeovery/claude-laravel
npx skills add https://github.com/leeovery/claude-laravel --skill laravel-testing使用 Pest 进行测试的模式:Arrange-Act-Assert(编排-执行-断言)、正确的模拟、空驱动、声明式工厂。
相关指南:
测试应该是:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
每个测试都应遵循 Arrange-Act-Assert(编排-执行-断言) 模式:
使用 工厂 设置所有需要的数据和依赖:
it('creates an order with items', function () {
// Arrange: 创建世界状态
$user = User::factory()->create();
$product = Product::factory()->active()->create(['price' => 1000]);
$data = CreateOrderData::from([
'customer_email' => 'customer@example.com',
'items' => [
['product_id' => $product->id, 'quantity' => 2],
],
]);
// Act: 执行操作
$order = resolve(CreateOrderAction::class)($user, $data);
// Assert: 验证结果
expect($order)
->toBeInstanceOf(Order::class)
->and($order->items)->toHaveCount(1)
->and($order->total)->toBe(2000);
});
执行你正在测试的 单一操作:
// ✅ 良好 - 单一、清晰的操作
$order = resolve(CreateOrderAction::class)($user, $data);
// ❌ 不佳 - 多个操作与断言混合
$order = resolve(CreateOrderAction::class)($user, $data);
expect($order)->toBeInstanceOf(Order::class);
$order->refresh();
expect($order->total)->toBe(2000);
验证你操作的 结果:
// ✅ 良好 - 清晰、专注的断言
expect($order)
->toBeInstanceOf(Order::class)
->and($order->status)->toBe(OrderStatus::Pending)
->and($order->items)->toHaveCount(2);
assertDatabaseHas('orders', [
'id' => $order->id,
'user_id' => $user->id,
]);
// ❌ 不佳 - 测试实现细节
expect($order->getAttribute('status'))->toBe('pending');
Actions 是 领域逻辑的核心,应在隔离环境中进行彻底测试。
use App\Actions\Order\CreateOrderAction;
use App\Data\CreateOrderData;
use App\Enums\OrderStatus;
use App\Models\User;
use function Pest\Laravel\assertDatabaseHas;
it('creates an order', function () {
// Arrange
$user = User::factory()->create();
$data = CreateOrderData::testFactory()->make([
'status' => OrderStatus::Pending,
]);
// Act
$order = resolve(CreateOrderAction::class)($user, $data);
// Assert
expect($order)->toBeInstanceOf(Order::class);
assertDatabaseHas('orders', [
'id' => $order->id,
'user_id' => $user->id,
'status' => OrderStatus::Pending->value,
]);
});
it('throws exception when user has too many pending orders', function () {
// Arrange
$user = User::factory()
->has(Order::factory()->pending()->count(5))
->create();
$data = CreateOrderData::testFactory()->make();
// Act & Assert
expect(fn () => resolve(CreateOrderAction::class)($user, $data))
->toThrow(OrderException::class, 'Too many pending orders');
});
关键模式: 始终使用 resolve() 从容器中解析 actions,以便递归解析依赖项。使用 swap() 将依赖项替换为模拟版本。
use function Pest\Laravel\mock;
use function Pest\Laravel\swap;
it('processes order and sends notification', function () {
// Arrange
$user = User::factory()->create();
$order = Order::factory()->for($user)->create();
// Mock the dependency actions and swap them into the container
$calculateTotal = mock(CalculateOrderTotalAction::class);
$calculateTotal->shouldReceive('__invoke')
->once()
->with($order)
->andReturn(10000);
swap(CalculateOrderTotalAction::class, $calculateTotal);
$notifyOrder = mock(NotifyOrderCreatedAction::class);
$notifyOrder->shouldReceive('__invoke')
->once()
->with($order);
swap(NotifyOrderCreatedAction::class, $notifyOrder);
// Act - resolve() from container so mocked dependencies are injected
$result = resolve(ProcessOrderAction::class)($order);
// Assert
expect($result->total)->toBe(10000);
});
为何采用此模式:
resolve() 确保从容器中拉取 action 及其所有依赖项swap() 将容器中的依赖项替换为你的模拟对象关键原则: 仅模拟你控制的代码。切勿直接模拟外部服务。
use function Pest\Laravel\mock;
use function Pest\Laravel\swap;
// Mock an action you own and swap it into the container
$sendEmail = mock(SendWelcomeEmailAction::class);
$sendEmail->shouldReceive('__invoke')
->once()
->with(Mockery::type(User::class));
swap(SendWelcomeEmailAction::class, $sendEmail);
// Then resolve the action under test - it will receive the mocked dependency
$result = resolve(RegisterUserAction::class)($data);
使用带有闭包的 withArgs() 来验证传递的 确切实例和值:
it('processes match with correct arguments', function () {
$matchAttempt = MatchAttempt::factory()->create();
$data = MatchData::testFactory()->make();
// Mock and verify exact arguments using expect() assertions
$mockAction = mock(CreateMatchResultAction::class);
$mockAction->shouldReceive('__invoke')
->once()
->withArgs(function (MatchAttempt $_matchAttempt, MatchData $_data) use ($data, $matchAttempt) {
// Verify the exact model instance is passed
expect($_matchAttempt->is($matchAttempt))->toBeTrue()
// Verify the exact DTO value is passed
->and($_data)->toBe($data->matches->first());
return true; // Return true to pass the assertion
});
swap(CreateMatchResultAction::class, $mockAction);
// Act
resolve(ProcessMatchAction::class)($matchAttempt, $data);
});
// Mock your own service through its facade
Payment::shouldReceive('createPaymentIntent')
->once()
->with(10000, 'usd')
->andReturn(PaymentIntentData::from([
'id' => 'pi_test_123',
'status' => 'succeeded',
]));
// ❌ DON'T DO THIS - Mocking Stripe SDK directly
$stripe = Mockery::mock(\Stripe\StripeClient::class);
$stripe->shouldReceive('paymentIntents->create')
->andReturn(/* ... */);
// This is brittle and breaks when Stripe updates their SDK
如果你发现自己需要模拟外部服务,创建一个抽象层:
完整实现示例请参阅 Services。
空驱动模式提供 确定性的快速测试,无需外部依赖:
it('processes payment successfully', function () {
// Arrange - Use null driver (configured in phpunit.xml or .env.testing)
Config::set('payment.default', 'null');
$order = Order::factory()->create(['total' => 10000]);
$data = PaymentData::from(['amount' => 10000, 'currency' => 'usd']);
// Act - No mocking needed, null driver returns test data
$payment = resolve(ProcessPaymentAction::class)($order, $data);
// Assert
expect($payment)
->toBeInstanceOf(Payment::class)
->and($payment->status)->toBe(PaymentStatus::Completed);
});
空驱动的优势:
为特定测试场景扩展空驱动:
// tests/Fakes/FailingPaymentDriver.php
class FailingPaymentDriver implements PaymentDriver
{
public function createPaymentIntent(int $amount, string $currency): PaymentIntentData
{
throw PaymentException::failedToCharge('Card declined');
}
}
// In test
it('handles payment failure gracefully', function () {
$this->app->bind(PaymentManager::class, function () {
$manager = new PaymentManager($this->app);
$manager->extend('failing', fn () => new FailingPaymentDriver);
return $manager;
});
Config::set('payment.default', 'failing');
$order = Order::factory()->create();
$data = PaymentData::testFactory();
expect(fn () => resolve(ProcessPaymentAction::class)($order, $data))
->toThrow(PaymentException::class, 'Card declined');
});
工厂创建 真实、随机的测试数据,使测试更健壮。
// Arrange with factories
$user = User::factory()->create();
$product = Product::factory()->active()->create();
$order = Order::factory()->for($user)->create();
// Factory with state
$pendingOrder = Order::factory()->pending()->create();
$paidOrder = Order::factory()->paid()->create();
// Factory with relationships
$user = User::factory()
->has(Order::factory()->count(3))
->create();
关键原则: 通过将数据库实现细节隐藏在工厂方法后面,使测试 声明式且可读。
// ❌ 不佳 - 数据库模式泄露到测试中
$calendar = Calendar::factory()->create([
'status' => 'accepted',
'reminder_sent_at' => null,
'approved_by' => User::factory()->create()->id,
'approved_at' => now(),
]);
// ✅ 良好 - 声明式且可读
$calendar = Calendar::factory()->accepted()->create();
DTO 应提供 测试工厂 以生成一致的测试数据:
class CreateOrderData extends Data
{
public function __construct(
public string $customerEmail,
public OrderStatus $status,
public array $items,
) {}
public static function testFactory(): self
{
return new self(
customerEmail: fake()->email(),
status: OrderStatus::Pending,
items: [
[
'product_id' => Product::factory()->create()->id,
'quantity' => fake()->numberBetween(1, 5),
],
],
);
}
}
// Usage in tests
$data = CreateOrderData::testFactory();
测试 完整的请求/响应周期:
use function Pest\Laravel\actingAs;
use function Pest\Laravel\postJson;
it('creates an order via API', function () {
$user = User::factory()->create();
$product = Product::factory()->create();
$response = actingAs($user)
->postJson('/api/orders', [
'customer_email' => 'test@example.com',
'items' => [
['product_id' => $product->id, 'quantity' => 2],
],
]);
$response->assertCreated()
->assertJsonStructure([
'data' => ['id', 'status', 'items'],
]);
});
在隔离环境中测试 领域逻辑:
it('calculates order total correctly', function () {
$order = Order::factory()->create();
$order->items()->createMany([
['price' => 1000, 'quantity' => 2],
['price' => 1500, 'quantity' => 1],
]);
$total = resolve(CalculateOrderTotalAction::class)($order);
expect($total)->toBe(3500);
});
脆弱测试在实现变更时会中断,即使行为是正确的。
// ✅ 良好 - 使用真实实例
it('calculates order total', function () {
$order = Order::factory()->create();
$order->items()->createMany([
['price' => 1000, 'quantity' => 2],
['price' => 500, 'quantity' => 1],
]);
$total = resolve(CalculateOrderTotalAction::class)($order);
expect($total)->toBe(2500);
});
// ❌ 不佳 - 模拟所有内容
it('calculates order total', function () {
$item1 = Mockery::mock(OrderItem::class);
$item1->shouldReceive('getPrice')->andReturn(1000);
// ... too much mocking
});
// ✅ 良好 - 测试行为
it('sends welcome email when user registers', function () {
Mail::fake();
$data = RegisterUserData::testFactory();
$user = resolve(RegisterUserAction::class)($data);
Mail::assertSent(WelcomeEmail::class, function ($mail) use ($user) {
return $mail->hasTo($user->email);
});
});
// ❌ 不佳 - 测试实现细节
it('sends welcome email when user registers', function () {
$mailer = Mockery::mock(Mailer::class);
$mailer->shouldReceive('send')
->with(Mockery::on(function ($email) {
return $email->template === 'emails.welcome';
}));
// Too specific, breaks if template name changes
});
// ✅ 良好 - 使用工厂
$user = User::factory()->create();
$data = ProfileData::testFactory();
// ❌ 不佳 - 硬编码数据
$data = new ProfileData(
firstName: 'John',
lastName: 'Doe',
phone: '555-1234',
bio: 'Test bio',
);
经验法则: 模拟协作者,而非数据。
// ✅ 良好 - 模拟通知服务(协作者)
$notifier = mock(NotificationService::class);
$notifier->shouldReceive('send')->once();
swap(NotificationService::class, $notifier);
resolve(ShipOrderAction::class)($order);
// ❌ 不佳 - 模拟数据(订单、用户)
$order = Mockery::mock(Order::class);
// ... mocking data objects makes test brittle
it('transitions order from pending to paid', function () {
$order = Order::factory()->pending()->create();
resolve(MarkOrderAsPaidAction::class)($order);
expect($order->fresh()->status)->toBe(OrderStatus::Paid)
->and($order->fresh()->paid_at)->not->toBeNull();
});
it('creates order with items', function () {
$user = User::factory()->create();
$products = Product::factory()->count(3)->create();
$data = CreateOrderData::from([
'customer_email' => 'test@example.com',
'items' => $products->map(fn ($p) => [
'product_id' => $p->id,
'quantity' => 2,
])->all(),
]);
$order = resolve(CreateOrderAction::class)($user, $data);
expect($order->items)->toHaveCount(3);
});
it('rolls back transaction on failure', function () {
$user = User::factory()->create();
$data = CreateOrderData::from([
'customer_email' => 'test@example.com',
'items' => [
['product_id' => 99999, 'quantity' => 1], // Non-existent product
],
]);
expect(fn () => resolve(CreateOrderAction::class)($user, $data))
->toThrow(Exception::class);
assertDatabaseCount('orders', 0);
assertDatabaseCount('order_items', 0);
});
use Illuminate\Support\Facades\Mail;
it('sends welcome email to new user', function () {
Mail::fake();
$data = RegisterUserData::testFactory();
$user = resolve(RegisterUserAction::class)($data);
Mail::assertSent(WelcomeEmail::class, function ($mail) use ($user) {
return $mail->hasTo($user->email);
});
});
use Illuminate\Support\Facades\Queue;
it('dispatches job to process order', function () {
Queue::fake();
$order = Order::factory()->create();
resolve(ProcessOrderAction::class)($order);
Queue::assertPushed(ProcessOrderJob::class, function ($job) use ($order) {
return $job->order->id === $order->id;
});
});
Calendar::factory()->accepted() 而非 ['status' => 'accepted']it('does something', function () {
// Arrange - Set up the world with declarative factories
$model = Model::factory()->active()->create();
$data = Data::testFactory();
// Act - Perform the operation
$result = resolve(Action::class)($model, $data);
// Assert - Verify the results
expect($result)->/* assertions */;
});
use function Pest\Laravel\mock;
use function Pest\Laravel\swap;
// Mock a dependency action
$mockAction = mock(YourDependencyAction::class);
$mockAction->shouldReceive('__invoke')
->once()
->with(/* expected params */)
->andReturn(/* return value */);
// Swap into container
swap(YourDependencyAction::class, $mockAction);
// Resolve action under test - container injects mocked dependencies
$result = resolve(ActionUnderTest::class)(/* params */);
use function Pest\Laravel\assertDatabaseHas;
use function Pest\Laravel\assertDatabaseCount;
assertDatabaseHas('orders', ['id' => $order->id]);
assertDatabaseCount('orders', 1);
每周安装数
171
仓库
GitHub 星标数
42
首次出现
2026年1月21日
安全审计
安装于
opencode155
codex153
gemini-cli146
github-copilot138
cursor130
kimi-cli119
Testing patterns with Pest: Arrange-Act-Assert, proper mocking, null drivers, declarative factories.
Related guides:
Testing should be:
Every test should follow the Arrange-Act-Assert pattern:
Set up all the data and dependencies needed using factories :
it('creates an order with items', function () {
// Arrange: Create the world state
$user = User::factory()->create();
$product = Product::factory()->active()->create(['price' => 1000]);
$data = CreateOrderData::from([
'customer_email' => 'customer@example.com',
'items' => [
['product_id' => $product->id, 'quantity' => 2],
],
]);
// Act: Perform the operation
$order = resolve(CreateOrderAction::class)($user, $data);
// Assert: Verify the results
expect($order)
->toBeInstanceOf(Order::class)
->and($order->items)->toHaveCount(1)
->and($order->total)->toBe(2000);
});
Perform the single operation you're testing:
// ✅ Good - Single, clear action
$order = resolve(CreateOrderAction::class)($user, $data);
// ❌ Bad - Multiple actions mixed with assertions
$order = resolve(CreateOrderAction::class)($user, $data);
expect($order)->toBeInstanceOf(Order::class);
$order->refresh();
expect($order->total)->toBe(2000);
Verify the outcomes of your action:
// ✅ Good - Clear, focused assertions
expect($order)
->toBeInstanceOf(Order::class)
->and($order->status)->toBe(OrderStatus::Pending)
->and($order->items)->toHaveCount(2);
assertDatabaseHas('orders', [
'id' => $order->id,
'user_id' => $user->id,
]);
// ❌ Bad - Testing implementation details
expect($order->getAttribute('status'))->toBe('pending');
Actions are the heart of your domain logic and should be thoroughly tested in isolation.
use App\Actions\Order\CreateOrderAction;
use App\Data\CreateOrderData;
use App\Enums\OrderStatus;
use App\Models\User;
use function Pest\Laravel\assertDatabaseHas;
it('creates an order', function () {
// Arrange
$user = User::factory()->create();
$data = CreateOrderData::testFactory()->make([
'status' => OrderStatus::Pending,
]);
// Act
$order = resolve(CreateOrderAction::class)($user, $data);
// Assert
expect($order)->toBeInstanceOf(Order::class);
assertDatabaseHas('orders', [
'id' => $order->id,
'user_id' => $user->id,
'status' => OrderStatus::Pending->value,
]);
});
it('throws exception when user has too many pending orders', function () {
// Arrange
$user = User::factory()
->has(Order::factory()->pending()->count(5))
->create();
$data = CreateOrderData::testFactory()->make();
// Act & Assert
expect(fn () => resolve(CreateOrderAction::class)($user, $data))
->toThrow(OrderException::class, 'Too many pending orders');
});
Critical pattern: Always resolve actions from the container using resolve() so dependencies are recursively resolved. Use swap() to replace dependencies with mocked versions.
use function Pest\Laravel\mock;
use function Pest\Laravel\swap;
it('processes order and sends notification', function () {
// Arrange
$user = User::factory()->create();
$order = Order::factory()->for($user)->create();
// Mock the dependency actions and swap them into the container
$calculateTotal = mock(CalculateOrderTotalAction::class);
$calculateTotal->shouldReceive('__invoke')
->once()
->with($order)
->andReturn(10000);
swap(CalculateOrderTotalAction::class, $calculateTotal);
$notifyOrder = mock(NotifyOrderCreatedAction::class);
$notifyOrder->shouldReceive('__invoke')
->once()
->with($order);
swap(NotifyOrderCreatedAction::class, $notifyOrder);
// Act - resolve() from container so mocked dependencies are injected
$result = resolve(ProcessOrderAction::class)($order);
// Assert
expect($result->total)->toBe(10000);
});
Why this pattern:
resolve() ensures the action is pulled from the container with all dependenciesswap() replaces the dependency in the container with your mockCritical principle: Only mock code that you control. Never mock external services directly.
use function Pest\Laravel\mock;
use function Pest\Laravel\swap;
// Mock an action you own and swap it into the container
$sendEmail = mock(SendWelcomeEmailAction::class);
$sendEmail->shouldReceive('__invoke')
->once()
->with(Mockery::type(User::class));
swap(SendWelcomeEmailAction::class, $sendEmail);
// Then resolve the action under test - it will receive the mocked dependency
$result = resolve(RegisterUserAction::class)($data);
Use withArgs() with a closure to verify the exact instances and values being passed:
it('processes match with correct arguments', function () {
$matchAttempt = MatchAttempt::factory()->create();
$data = MatchData::testFactory()->make();
// Mock and verify exact arguments using expect() assertions
$mockAction = mock(CreateMatchResultAction::class);
$mockAction->shouldReceive('__invoke')
->once()
->withArgs(function (MatchAttempt $_matchAttempt, MatchData $_data) use ($data, $matchAttempt) {
// Verify the exact model instance is passed
expect($_matchAttempt->is($matchAttempt))->toBeTrue()
// Verify the exact DTO value is passed
->and($_data)->toBe($data->matches->first());
return true; // Return true to pass the assertion
});
swap(CreateMatchResultAction::class, $mockAction);
// Act
resolve(ProcessMatchAction::class)($matchAttempt, $data);
});
// Mock your own service through its facade
Payment::shouldReceive('createPaymentIntent')
->once()
->with(10000, 'usd')
->andReturn(PaymentIntentData::from([
'id' => 'pi_test_123',
'status' => 'succeeded',
]));
// ❌ DON'T DO THIS - Mocking Stripe SDK directly
$stripe = Mockery::mock(\Stripe\StripeClient::class);
$stripe->shouldReceive('paymentIntents->create')
->andReturn(/* ... */);
// This is brittle and breaks when Stripe updates their SDK
If you find yourself needing to mock an external service, create an abstraction :
See Services for complete implementation examples.
The null driver pattern provides deterministic, fast tests without external dependencies:
it('processes payment successfully', function () {
// Arrange - Use null driver (configured in phpunit.xml or .env.testing)
Config::set('payment.default', 'null');
$order = Order::factory()->create(['total' => 10000]);
$data = PaymentData::from(['amount' => 10000, 'currency' => 'usd']);
// Act - No mocking needed, null driver returns test data
$payment = resolve(ProcessPaymentAction::class)($order, $data);
// Assert
expect($payment)
->toBeInstanceOf(Payment::class)
->and($payment->status)->toBe(PaymentStatus::Completed);
});
Benefits of null drivers:
Extend the null driver for specific test scenarios:
// tests/Fakes/FailingPaymentDriver.php
class FailingPaymentDriver implements PaymentDriver
{
public function createPaymentIntent(int $amount, string $currency): PaymentIntentData
{
throw PaymentException::failedToCharge('Card declined');
}
}
// In test
it('handles payment failure gracefully', function () {
$this->app->bind(PaymentManager::class, function () {
$manager = new PaymentManager($this->app);
$manager->extend('failing', fn () => new FailingPaymentDriver);
return $manager;
});
Config::set('payment.default', 'failing');
$order = Order::factory()->create();
$data = PaymentData::testFactory();
expect(fn () => resolve(ProcessPaymentAction::class)($order, $data))
->toThrow(PaymentException::class, 'Card declined');
});
Factories create realistic, randomized test data that makes tests more robust.
// Arrange with factories
$user = User::factory()->create();
$product = Product::factory()->active()->create();
$order = Order::factory()->for($user)->create();
// Factory with state
$pendingOrder = Order::factory()->pending()->create();
$paidOrder = Order::factory()->paid()->create();
// Factory with relationships
$user = User::factory()
->has(Order::factory()->count(3))
->create();
Critical principle: Make tests declarative and readable by hiding database implementation details behind factory methods.
// ❌ Bad - Database schema leaks into test
$calendar = Calendar::factory()->create([
'status' => 'accepted',
'reminder_sent_at' => null,
'approved_by' => User::factory()->create()->id,
'approved_at' => now(),
]);
// ✅ Good - Declarative and readable
$calendar = Calendar::factory()->accepted()->create();
→ Complete declarative factory patterns: testing-factories.md
DTOs should provide test factories for consistent test data:
class CreateOrderData extends Data
{
public function __construct(
public string $customerEmail,
public OrderStatus $status,
public array $items,
) {}
public static function testFactory(): self
{
return new self(
customerEmail: fake()->email(),
status: OrderStatus::Pending,
items: [
[
'product_id' => Product::factory()->create()->id,
'quantity' => fake()->numberBetween(1, 5),
],
],
);
}
}
// Usage in tests
$data = CreateOrderData::testFactory();
Test the complete request/response cycle :
use function Pest\Laravel\actingAs;
use function Pest\Laravel\postJson;
it('creates an order via API', function () {
$user = User::factory()->create();
$product = Product::factory()->create();
$response = actingAs($user)
->postJson('/api/orders', [
'customer_email' => 'test@example.com',
'items' => [
['product_id' => $product->id, 'quantity' => 2],
],
]);
$response->assertCreated()
->assertJsonStructure([
'data' => ['id', 'status', 'items'],
]);
});
Test domain logic in isolation :
it('calculates order total correctly', function () {
$order = Order::factory()->create();
$order->items()->createMany([
['price' => 1000, 'quantity' => 2],
['price' => 1500, 'quantity' => 1],
]);
$total = resolve(CalculateOrderTotalAction::class)($order);
expect($total)->toBe(3500);
});
Brittle tests break when implementation changes, even if behavior is correct.
// ✅ Good - Use real instances
it('calculates order total', function () {
$order = Order::factory()->create();
$order->items()->createMany([
['price' => 1000, 'quantity' => 2],
['price' => 500, 'quantity' => 1],
]);
$total = resolve(CalculateOrderTotalAction::class)($order);
expect($total)->toBe(2500);
});
// ❌ Bad - Mock everything
it('calculates order total', function () {
$item1 = Mockery::mock(OrderItem::class);
$item1->shouldReceive('getPrice')->andReturn(1000);
// ... too much mocking
});
// ✅ Good - Test the behavior
it('sends welcome email when user registers', function () {
Mail::fake();
$data = RegisterUserData::testFactory();
$user = resolve(RegisterUserAction::class)($data);
Mail::assertSent(WelcomeEmail::class, function ($mail) use ($user) {
return $mail->hasTo($user->email);
});
});
// ❌ Bad - Test implementation details
it('sends welcome email when user registers', function () {
$mailer = Mockery::mock(Mailer::class);
$mailer->shouldReceive('send')
->with(Mockery::on(function ($email) {
return $email->template === 'emails.welcome';
}));
// Too specific, breaks if template name changes
});
// ✅ Good - Use factories
$user = User::factory()->create();
$data = ProfileData::testFactory();
// ❌ Bad - Hardcoded data
$data = new ProfileData(
firstName: 'John',
lastName: 'Doe',
phone: '555-1234',
bio: 'Test bio',
);
Rule of thumb: Mock collaborators, not data.
// ✅ Good - Mock the notification service (collaborator)
$notifier = mock(NotificationService::class);
$notifier->shouldReceive('send')->once();
swap(NotificationService::class, $notifier);
resolve(ShipOrderAction::class)($order);
// ❌ Bad - Mock the data (order, user)
$order = Mockery::mock(Order::class);
// ... mocking data objects makes test brittle
it('transitions order from pending to paid', function () {
$order = Order::factory()->pending()->create();
resolve(MarkOrderAsPaidAction::class)($order);
expect($order->fresh()->status)->toBe(OrderStatus::Paid)
->and($order->fresh()->paid_at)->not->toBeNull();
});
it('creates order with items', function () {
$user = User::factory()->create();
$products = Product::factory()->count(3)->create();
$data = CreateOrderData::from([
'customer_email' => 'test@example.com',
'items' => $products->map(fn ($p) => [
'product_id' => $p->id,
'quantity' => 2,
])->all(),
]);
$order = resolve(CreateOrderAction::class)($user, $data);
expect($order->items)->toHaveCount(3);
});
it('rolls back transaction on failure', function () {
$user = User::factory()->create();
$data = CreateOrderData::from([
'customer_email' => 'test@example.com',
'items' => [
['product_id' => 99999, 'quantity' => 1], // Non-existent product
],
]);
expect(fn () => resolve(CreateOrderAction::class)($user, $data))
->toThrow(Exception::class);
assertDatabaseCount('orders', 0);
assertDatabaseCount('order_items', 0);
});
use Illuminate\Support\Facades\Mail;
it('sends welcome email to new user', function () {
Mail::fake();
$data = RegisterUserData::testFactory();
$user = resolve(RegisterUserAction::class)($data);
Mail::assertSent(WelcomeEmail::class, function ($mail) use ($user) {
return $mail->hasTo($user->email);
});
});
use Illuminate\Support\Facades\Queue;
it('dispatches job to process order', function () {
Queue::fake();
$order = Order::factory()->create();
resolve(ProcessOrderAction::class)($order);
Queue::assertPushed(ProcessOrderJob::class, function ($job) use ($order) {
return $job->order->id === $order->id;
});
});
Calendar::factory()->accepted() not ['status' => 'accepted']it('does something', function () {
// Arrange - Set up the world with declarative factories
$model = Model::factory()->active()->create();
$data = Data::testFactory();
// Act - Perform the operation
$result = resolve(Action::class)($model, $data);
// Assert - Verify the results
expect($result)->/* assertions */;
});
use function Pest\Laravel\mock;
use function Pest\Laravel\swap;
// Mock a dependency action
$mockAction = mock(YourDependencyAction::class);
$mockAction->shouldReceive('__invoke')
->once()
->with(/* expected params */)
->andReturn(/* return value */);
// Swap into container
swap(YourDependencyAction::class, $mockAction);
// Resolve action under test - container injects mocked dependencies
$result = resolve(ActionUnderTest::class)(/* params */);
use function Pest\Laravel\assertDatabaseHas;
use function Pest\Laravel\assertDatabaseCount;
assertDatabaseHas('orders', ['id' => $order->id]);
assertDatabaseCount('orders', 1);
Weekly Installs
171
Repository
GitHub Stars
42
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode155
codex153
gemini-cli146
github-copilot138
cursor130
kimi-cli119
测试策略完整指南:单元/集成/E2E测试金字塔与自动化实践
11,200 周安装