Laravel单元测试报错:调用null的findOne方法,接口注入遇问题
排查Laravel单元测试中UserRepositoryInterface注入失败的问题
针对你遇到的Call to a member function findOne() on null错误,本质是UserRepositoryInterface的实例未被正确注入到UserService中,导致调用findOne()时对象为null。可以按以下步骤逐一排查:
确认接口与实现的绑定配置
检查RepositoryServiceProvider的register方法,是否正确完成接口到实现类的绑定:public function register() { $this->app->bind(UserRepositoryInterface::class, UserRepository::class); }同时要确保该Provider已添加到
config/app.php的providers数组中,Laravel启动时才会加载这条绑定规则。测试类需继承Laravel的TestCase
你的AuthControllerTest必须继承Tests\TestCase而非原生PHPUnit测试类。只有继承该类,测试环境才会初始化Laravel服务容器,依赖注入机制才能正常运转:use Tests\TestCase; class AuthControllerTest extends TestCase { // ... 测试方法 }避免直接实例化服务类
不要在测试中直接new UserService(),这种方式不会触发Laravel的依赖注入。应通过容器解析实例:// 正确方式:通过容器获取UserService实例 $userService = $this->app->make(UserService::class); // 或者直接解析控制器,容器会自动处理整条依赖链 $controller = $this->app->make(AuthController::class);单元测试中Mock接口(可选)
如果是纯单元测试,不想依赖数据库或实际Repository实现,可以MockUserRepositoryInterface并手动注入到UserService:use Mockery; public function testSomething() { // Mock接口实例 $mockRepo = Mockery::mock(UserRepositoryInterface::class); // 预设findOne方法的返回逻辑 $mockRepo->shouldReceive('findOne')->with(/* 目标参数 */)->andReturn(/* 模拟的用户对象 */); // 手动注入Mock实例到UserService $userService = new UserService($mockRepo); // 后续测试逻辑 }检查类名与命名空间
确认UserRepositoryInterface、UserRepository的命名空间是否正确,构造函数中的类型提示有没有写错类名或命名空间,避免因类找不到导致注入失败。
内容的提问来源于stack exchange,提问作者khanam
相关产品推荐
相关产品推荐

