Skip to content

Dependency injection not working during testsย #76

@Spartaques

Description

@Spartaques

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions