Skip to content

@MainActor protocol generates mock that doesn't compile #338

@onelittlefish

Description

@onelittlefish

New Issue Checklist

Overview

If a protocol is annotated with @MainActor, the generated mock is not main actor-isolated and doesn't compile.

Example

Source:

@MainActor
protocol MyProtocol {
    func foo()
}

Generated:

public func mock(_ type: Example.MyProtocol.Protocol, file: StaticString = #file, line: UInt = #line) -> MyProtocolMock {
    return MyProtocolMock(sourceLocation: Mockingbird.SourceLocation(file, line))
}

The generated code results in a compile error:

Call to main actor-isolated initializer 'init(sourceLocation:)' in a synchronous nonisolated context

Expected Behavior

I think the generated mock() function needs to be @MainActor.

Environment

  • Mockingbird CLI version (0.20.0)
  • Xcode and Swift version (swift --version)
  • Package manager (SPM project)
  • Unit testing framework (XCTest)
  • Custom configuration
    • Mockingbird ignore files
    • Supporting source files

Metadata

Metadata

Assignees

No one assigned

    Labels

    generator bugCauses malformed generated code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions