mockingbird icon indicating copy to clipboard operation
mockingbird copied to clipboard

Unable to resolve inherited type 'AnyObject'

Open lhunath opened this issue 2 years ago • 3 comments

New Issue Checklist

Overview

When a protocol extends AnyObject, the generator does generate a mock for the protocol but the generated mock does not adopt the protocol it is generating for. As a result, we have a mock whose type does not conform the protocol and is unusable in practice.

Example

Failure scenario, UserProject: AnyObject yields a UserProjectMock which does not : UserProject:

public protocol UserProject: AnyObject {
    var isFavorite: Bool { get set }
}

// MARK: - Mocked UserProject
public final class UserProjectMock: Mockingbird.Mock {
  typealias MockingbirdSupertype = Projects.UserProject
  public static let mockingbirdContext = Mockingbird.Context()
  public let mockingbirdContext = Mockingbird.Context(["generator_version": "0.20.0", "module_name": "Projects"])
}

Success scenario, bare UserProject yields a UserProjectMock which does : UserProject:

public protocol UserProject {
    var isFavorite: Bool { get set }
}

public final class UserProjectMock: Projects.UserProject, Mockingbird.Mock {
  typealias MockingbirdSupertype = Projects.UserProject
  public static let mockingbirdContext = Mockingbird.Context()
  public let mockingbirdContext = Mockingbird.Context(["generator_version": "0.20.0", "module_name": "Projects"])

  // MARK: Mocked isFavorite
  public var `isFavorite`: Bool {
    ...
  }
}

Expected Behavior

The conformance of a protocol to : AnyObject should not affect the mock generation since the mock is already a class type.

Environment

  • Mockingbird CLI version: 0.20.0
  • Xcode and Swift version: swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
  • Package manager: SPM
  • Unit testing framework: XCTest

lhunath avatar Mar 13 '23 21:03 lhunath

Running with --verbose, we see:

Unable to resolve inherited type 'AnyObject' for 'UserProject'

lhunath avatar Mar 13 '23 21:03 lhunath

Work-around, add this to MockingbirdSupport/Swift/AnyObject.swift:

protocol AnyObject {}

lhunath avatar Mar 13 '23 21:03 lhunath

https://github.com/birdrides/mockingbird/issues/321

Narayane avatar May 03 '23 09:05 Narayane