Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pradnya-nikam opened this issue Jul 13, 2021 · 1 comment

Comments

@pradnya-nikam
Copy link

pradnya-nikam commented Jul 13, 2021

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?

@MatyasKriz
Copy link
Collaborator

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants