Skip to content

Registered mock not resolved in Task.detached #344

@cassianomonteiro

Description

@cassianomonteiro

In the example below I have an injected dependency that is used within a Task.detached. Looks like the container resolves the regular production instance instead of the mock registered in the test in this case. If I use Task instead of Task.detached, the mock instance is resolved correctly during tests. Is this the expected behaviour? How is the intended usage of Factory in this case of a detached task? Thanks!

Code:

  func foo() {
      Task.detached {
        let api = Container.shared.api()
        let log = ErrorLog(content: "\(error)")
        await api.send(log: log)
      }
  }

Test

@Suite(.container)
final class MyTests {

  private let mockAPI = APIMock()

  init() {
    Container.shared.api.register { self.mockAPI }
  }

  @Test
  func someTest() async throws {
   // do something that calls foo() <-- This does not resolve the mock instance if foo is using Task.detached
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions