Skip to content

Dependency injection not working during tests #76

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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