Does anyone have any tips for mocking classes defi...
# multiplatform
s
Does anyone have any tips for mocking classes defined in Kotlin from Swift? Context being that I'm writing a Swift class that takes a Kotlin dependency. This works perfectly in production but in tests I'd like to be able to mock out that kotlin dependency
m
you can declare an interface for that class in kotlin. it will become a swift protocol. and then you can use that protocol for ios mock / fake
s
Yeah that's what I've done but can't find a mocking framework that will auto-generate the mock, do you have any examples?
m
that's a general pain point with ios development, not related to kotlin native. afaik there's no mockk-like stuff, because swift doesn't fully support reflection
theoretically these are obj-c classes so they could be mocked... but I don't think anyone created such a framework, because there wouldn't be much of a market for it
s
There's a few decent swift mocking libraries these days, mockingbird for example, (though like you said they work off a different principle to most due to the lack of reflection) My question was more whether someone had created a framework that allows for mocking the classes defined in Kotlin
f
Have you found any solution for this? I am struggling with the same problem. I want to generate mocks for interface declared in Kotlin common code.
s
I changed my architecture. I now have all my ViewModels defined in common Kotlin code, so only my Views are in swift. This eliminates my need for mocking kotlin code since I don't need to inject mocks into my views for testing, doesn't answer your question but hope it helps!
f
There is no way for me to push my team to write shared view models now 😄 So it does not help but thanks for replying.
m
swifty-mocky doesn't work on protocols from kotlin? never tried
f
I don't think so because you have to annotate the protocols that will be mocked. I don't think you can do that in Kotlin 🤔
m
True... our ios team was exploring an idea to create mocks of classes by generating their extensions, instead of class implementations of protocols - with an api kind of like swifty mocky has but without the need to use protocols. The idea is to use sourcery to create them. I wonder if that idea could be leveraged for this problem as well. E.g.
Copy code
protocol from kotlin -> annotated extension of that protocol (just so you have something to add the annotation to) -> generated extension which mocks behaviors of the protocol
g
We use mockingbird. We had to 1. make sure the iOS app is using `interface`_s_ from shared code, e.g.
interface SharedCodeInterface
2. manually convert those `interface`s to Swift protocols and put them in files under dir
MockingSupport/<shared module's framework name>/
, e.g.
public protocol SharedCodeInterface { ... }
3. write protocols in our tests that conform to the protocols of step 2, e.g.
public protocol TestSharedCodeInterface : SharedCodeInterface {}
4. use the protocols of step 4 in our tests, i.e.
mock(TestSharedCodeInterface.self)
5. set mockingbird's run script in Build phases to generate mock classes for code in tests too (i.e.
--targets appTarget testsTarget
)