hyperf / hyperf/hyperf-skeleton

Dependency injection not working during tests

Open
#76 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
306
Forks
130
PR merge metrics
No merged PRs in 30d

Description

Hello guys. I have a controller called UserController. And i have a UserService that call getById method that returns some object. How can i mock this call when i test UserController?

I tries this code
` /** @var \Hyperf\Di\Container $c */
$c = \Hyperf\Utils\ApplicationContext::getContainer();

$queryServiceMock = $this->createMock(QueryService::class);

$queryServiceMock->method('appByKeyAndDomain')->willReturn((object)[
'id' => 1,
'key' => 'test',
'timezone' => 'UTC'
]);

$c->set(QueryService::class, $queryServiceMock);

\Hyperf\Utils\ApplicationContext::setContainer($c);`

but it would not work.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.