uber / mockolo

Efficient Mock Generator for Swift
Apache License 2.0
805 stars 85 forks source link

Actor annotated protocols don't work #171

Open vsanthanam opened 2 years ago

vsanthanam commented 2 years ago

If you annotate a protocol with a global actor, such as @MainActor, its generate mock doesn't compile:

Given

/// @mockable
@MainActor
protocol RootController: AnyObject {
    var viewController: UIViewController { get }
}

the mock will fail with this compilation error:


❌ /usr/local/var/buildkite-agent/builds/7003-local-1/varun-santhanam/{}/Project/{}Tests/Mocks.swift:226:16: call to main actor-isolated initializer 'init()' in a synchronous nonisolated context
--
  | return RootControllerMock()

The solution, I believe, would be to also add the actor annotation to the Mock

An identically generated mock, with the @MainActor annotation would work just fine.

I believe there would be similar errors for per-member annotations as well.

uhooi commented 2 years ago

@vsanthanam I can generate mocks with no problem. Would the same error occur if you use the latest version of Mockolo (1.7.0)?

vsanthanam commented 2 years ago

Seems to be happening even on 1.7 note that generating the mocks isn't the problem. Its that the mocks aren't compilable.

❌ /Users/vsanthanam/Desktop/convusic-monorepo/Project/ConvusicMobileTests/Mocks.swift:498:16: call to main actor-isolated initializer 'init()' in a synchronous nonisolated context
        return MainControllerMock()

The mocks cannot invoke the initializers of their actor dependent mocks because they are not annotated correctly

Here's an attached generated but non-compiling example: Mocks.swift.txt