Dependency injection not working during tests
還沒有人認領這個 Issue。
評估
研究方向
從 UserController 測試以及 issue 中展示的相依性注入設定開始,然後追蹤 QueryService 在測試期間如何被解析。重現失敗的 mock 呼叫,並比較 controller 使用的容器與正在被修改的容器。當 controller 收到 mock 的 QueryService,且測試驗證了預期傳回的物件時,即表示完成。
由索引模型根據 Issue 內容生成。
描述
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.
- 主要語言
- PHP
- 星號
- 307
- 分支
- 131
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 提供 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 沒有貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
hyperf/hyperf-skeleton 的其他 Issue
-
難度 3/5 1-2 天 新手友好度 30/100
hyperf/hyperf-skeleton#106 · 1 則留言 ·
查看 hyperf/hyperf-skeleton 的全部 Issue
相似的 Issue
-
enhancement
難度 2/5 1-3 小時 新手友好度 83/100
Phauthentic/cognitive-code-analysis#94 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 95/100
My-Little-Forum/mylittleforum#936 ·
-
bug
難度 1/5 1 小時以內 新手友好度 84/100
mailcow/mailcow-dockerized#7652 · 2 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100