Cuckoo icon indicating copy to clipboard operation
Cuckoo copied to clipboard

Unable to mock a class with immutable properties that implements a protocol

Open pradnya-nikam opened this issue 4 years ago • 1 comments

Hello! Cuckoo works well to create mocks for some of my simpler classes, but it is not working for models that have a pretty complicated hierarchy - appreciate if someone can help me get around this problem!

For classes that implement a protocol, cuckoo tries to override read only properties in generated mocks which doesn't compile. Best to explain with an example.

eg


protocol DemoProtocol {
  var id: String { get }
}

class Demo: DemoProtocol {
  let id: String
  let name: String
  init(id: String, name: String) {
    self.id = id
    self.name = name
  }
}

Since the protocol needs to declare id as a var (Swift protocols cannot require properties to be immutable), Cuckoo tries to override id, which of course doesn't compile.

Generated mocks for this file:


class MockDemo: Demo, Cuckoo.ClassMock {
    
     typealias MocksType = Demo
    
     typealias Stubbing = __StubbingProxy_Demo
     typealias Verification = __VerificationProxy_Demo

     let cuckoo_manager = Cuckoo.MockManager.preconfiguredManager ?? Cuckoo.MockManager(hasParent: true)

    
    private var __defaultImplStub: Demo?

     func enableDefaultImplementation(_ stub: Demo) {
        __defaultImplStub = stub
        cuckoo_manager.enableDefaultStubImplementation()
    }
    

    
    
    
     override var id: String {   //⚠️ Compile error: Cannot override immutable 'let' property 'id' with the getter of a 'var'
        get {
            return cuckoo_manager.getter("id",
                superclassCall:
                    
                    super.id
                    ,
                defaultCall: __defaultImplStub!.id)
        }
        
    }
    

Compiler error:

Error: Cannot override immutable 'let' property 'id' with the getter of a 'var'

Any advice on how to get around this?

pradnya-nikam avatar Jul 13 '21 07:07 pradnya-nikam

Hey, @pradnya-nikam. That's a good point, though I'm not really sure how to solve this. @TadeasKriz do you have an idea if that's possible?

MatyasKriz avatar Jul 22 '21 12:07 MatyasKriz