laravel中使用phpUnit模拟对象可隔离外部依赖,提升测试速度与专注度。1. 通过Facade的shouldReceive或fake方法模拟Cache、Mail等门面调用;2. 使用$this->mock()模拟服务容器中的类,实现依赖替换;3. 采用partialMock仅拦截特定方法,保留其余逻辑;4. 利用Event::fake()、mail::fake()、Notification::fake()断言事件、邮件、通知行为;5. 结合Mockery手动创建复杂模拟对象,并通过$app->instance()注入容器。合理运用这些方式可使测试更快速稳定。

Laravel 中使用 PHPUnit 模拟对象,主要是为了在测试过程中隔离外部依赖,比如数据库、队列、第三方服务等,从而让测试更快速、更专注。Laravel 基于 PHPUnit,并集成了强大的 Mockery 集成支持,让你可以轻松创建模拟对象。
1. 使用 Laravel 的 Facade 模拟
当你的代码中使用了 Laravel 的门面(Facade),比如 Cache::get()、Mail::send() 或 http::get(),你可以使用 Fake 或 shouldReceive() 来模拟它们的行为。
示例:模拟 Cache 门面
use IlluminateSupportFacadesCache; public function test_cache_is_used() { Cache::shouldReceive('get') ->with('user_count') ->andReturn(100); $response = $this->get('/stats'); $response->assertSee('100 users'); }
上面的代码告诉 Laravel:当调用 Cache::get(‘user_count’) 时,返回 100,而不是真实访问缓存。
立即学习“PHP免费学习笔记(深入)”;
2. 模拟服务容器中的类
如果你在控制器或服务中依赖注入了一个服务类,可以使用 $this->mock() 或 $this->partialMock() 方法来模拟这个类。
示例:模拟 UserService 类
use AppServicesUserService; public function test_user_profile_is_shown() { $this->mock(UserService::class, function ($mock) { $mock->shouldReceive('getProfile') ->with(1) ->andReturn(['name' => 'John']); }); $response = $this->get('/profile/1'); $response->assertjson(['name' => 'John']); }
这样,当 Laravel 容器解析 UserService 时,会自动使用你定义的模拟对象。
3. 部分模拟(Partial Mock)
有时候你只想模拟类中的某个方法,其他方法仍执行原逻辑,这时使用 partialMock。
$this->partialMock(UserService::class, function ($mock) { $mock->shouldReceive('sendNotification')->once(); });
这表示只拦截 sendNotification 方法,其余方法保持真实调用。
4. 模拟事件、邮件、通知
Laravel 提供了专门的测试辅助函数来模拟常见功能:
- Event::fake() —— 禁止事件广播,可断言是否触发
- Mail::fake() —— 拦截邮件发送
- Notification::fake() —— 模拟通知
示例:验证邮件是否发送
use IlluminateSupportFacadesMail; use AppMailWelcomeEmail; public function test_welcome_email_is_sent() { Mail::fake(); $response = $this->post('/register', [ 'email' => 'test@example.com' ]); Mail::assertSent(WelcomeEmail::class, function ($mail) { return $mail->hasTo('test@example.com'); }); }
5. 使用 Mockery 手动创建 Mock 对象
你也可以直接使用 Mockery 创建更复杂的模拟场景。
$mock = Mockery::mock('AppServicesPaymentgateway'); $mock->shouldReceive('charge')->andReturn(true); $this->app->instance(PaymentGateway::class, $mock);
通过 $this->app->instance() 将模拟对象绑定到服务容器,替换原始实现。
基本上就这些。Laravel 的测试系统让模拟对象变得简单直观,关键是理解何时使用 shouldReceive、fake 和容器绑定。合理使用模拟,能让测试更快更稳定。


