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

custom initializers causing errors in generated mock file #54

Closed
bobbelkl opened this issue Aug 25, 2016 · 14 comments
Closed

custom initializers causing errors in generated mock file #54

bobbelkl opened this issue Aug 25, 2016 · 14 comments

Comments

@bobbelkl
Copy link

following code

  init(userName: String, password: String)  {
    self.userName = userName
    self.password = password
    self.sessionId = (self.userName+self.password).hash.description
  }

generates this:

    override init(userName userName: String, password: String) {
        return manager.call("init(userName:String, password:String)", parameters: (userName: userName, password: password), original: observed.map { o in return { (userName userName: String, password: String) in o.init(userName: userName, password: password) } })
    }

removing the double designation doesn't help

@krin-san
Copy link

krin-san commented Aug 31, 2016

@bobbelkl, I experienced the same issue: #6 (comment)
Let's keep this ticket opened to track a code generator issue progress.

Right now I'm waiting for the refactoring branch to be merged and ready to test.

@TadeasKriz
Copy link
Member

Hey guys, I just merged the overhaul PR, but I would like to wait with the release until we fix these issues. However you can give it a try, just add the CocoaPods dependency as a git repository, targeting the master branch. We have made a lot of changes and also merged the runtime and generator repository for easier updates and usage.

@krin-san
Copy link

@TadeasKriz, I re-generated mocks file for the following class using Cuckoo from the commit c9fce39. Still no success with generated closure.

class MyClass {
    func objects(byFilter filter: String) -> String {
        return ""
    }
}
class MockMyClass: MyClass, Cuckoo.Mock {
    typealias Stubbing = __StubbingProxy_MyClass
    typealias Verification = __VerificationProxy_MyClass
    let manager = Cuckoo.MockManager()

    private var observed: MyClass?

    required override init() {
    }

    required init(spyOn victim: MyClass) {
        observed = victim
    }

    override func objects(byFilter filter: String) -> String {
        return manager.call("objects(byFilter: String) -> String", parameters: (filter), original: observed.map { o in return { (byFilter filter: String) -> String in o.objects(byFilter: filter) } })
    }

    struct __StubbingProxy_MyClass: Cuckoo.StubbingProxy {
        ...
    }

    struct __VerificationProxy_MyClass: Cuckoo.VerificationProxy {
        ...
    }
}

The following declaration is not a valid closure syntax:

(byFilter filter: String) -> String

@TadeasKriz
Copy link
Member

@bobbelkl @krin-san Hey guys, please give it a try. Use commit c7dd503669f1c1cf799aba5d2cfa9b93af64313f in your Podfile and if it works as expected, we'll release a new version :) Thanks.

@bobbelkl
Copy link
Author

bobbelkl commented Sep 7, 2016

Works for me!
All initializers are ignored, as they should.

Thanks!

@TadeasKriz
Copy link
Member

@bobbelkl Awesome!

@bobbelkl
Copy link
Author

bobbelkl commented Sep 7, 2016

Ok, found some issues.
I was mistaken stating the custom initializers are ignored, instead they become the default initializers, additionally the spyOn initializer is gone.

In my eyes the more elegant solution would be ignoring the custom initializers in classes. If you really need them you can still use "spyOn".

@TadeasKriz
Copy link
Member

We can't ignore custom initializers, because when initializing the mock, we have to call super.init. The spyOn is not possible to implement, because you need additional parameters to initialize the parent class.

A possible way to solve this is, that we don't need the spyOn at all when working with classes, because you have the class already instantiated. The spyOn is made primarily for protocols, because you might want to pass in a real implementation and just ensure it was called as expected. Mock created from a class already has this real implementation hiding in the parent class. So we might just add some switch that will enable passthrough to parent for every method. What do you think?

@TadeasKriz
Copy link
Member

@bobbelkl I just got an idea how to workaround the spyOn problem, there will be a new commit shortly, I'll let you know when it'll be ready for testing.

@bobbelkl
Copy link
Author

bobbelkl commented Sep 7, 2016

You're right, i forgot about super.init and everything it implies.
Very curious about your workaround. Looking forward to!

@TadeasKriz
Copy link
Member

@bobbelkl Here it is, try the 4a2a72fac947e8ddd0d441d8b42ab964915d3b68 commit. To make a spying mock, do:
let spyingMock = MockSomeClass(customInitParam: "XX").spy(on: realInstance). Please let me know if it work, thanks :)

@bobbelkl
Copy link
Author

bobbelkl commented Sep 7, 2016

Great work
I like that one!
Not it covers all I need.

@krin-san
Copy link

krin-san commented Sep 8, 2016

@TadeasKriz, now the generator works fine with all examples I shown before.
Thanks!

@TadeasKriz
Copy link
Member

Version 0.7.0 released! Yay!

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

3 participants