kgurgul
11/14/2024, 8:44 AMverify
method for KMP without using mocking libraries?dave08
11/14/2024, 11:40 AMdave08
11/14/2024, 11:41 AMkgurgul
11/14/2024, 11:56 AMdave08
11/14/2024, 11:57 AMkgurgul
11/14/2024, 11:57 AMdave08
11/14/2024, 11:59 AMdave08
11/14/2024, 12:00 PMkgurgul
11/14/2024, 12:00 PMdave08
11/14/2024, 12:01 PMkgurgul
11/14/2024, 12:01 PMdave08
11/14/2024, 12:01 PMdave08
11/14/2024, 12:02 PMdave08
11/14/2024, 12:02 PMdave08
11/14/2024, 12:03 PMJavier
11/14/2024, 1:37 PMverify
is an antipattern, tests should check the output based on an input, not checking if a method has or hasn't been calleddave08
11/14/2024, 2:39 PMJavier
11/14/2024, 2:40 PMdave08
11/14/2024, 2:41 PMdave08
11/14/2024, 2:42 PMdave08
11/14/2024, 2:43 PMJavier
11/14/2024, 2:43 PMdave08
11/14/2024, 2:44 PMfun updateRss(...) {
// Check if updating is needed
if (...) {
rssUpdater.update(...)
}
}
Javier
11/14/2024, 2:45 PMJavier
11/14/2024, 2:46 PMdave08
11/14/2024, 2:47 PMdave08
11/14/2024, 2:49 PMdave08
11/14/2024, 2:53 PMdave08
11/14/2024, 2:54 PMJavier
11/14/2024, 3:26 PMkgurgul
11/14/2024, 3:35 PMJavier
11/14/2024, 3:37 PMJavier
11/14/2024, 3:40 PMverify
on those methods.
The sample about publishing to Maven is exactly that. It has a lot of third party methods to configure the publication. I don't care at all about when and how they are called. I just care about the output.kgurgul
11/14/2024, 4:09 PMSystem.gc()
is called but you want to verify if cleanup()
is called in some conditions here https://github.com/kamgurgul/cpu-info/blob/master/shared/src/commonMain/kotlin/com/kgurgul/cpuinfo/domain/action/RamCleanupAction.kt#L9
There is no visible outcome which you can check. Of course this is a simple case but the idea would be the same if for example method doWork
would have more complex logic which should be tested.dave08
11/14/2024, 4:12 PMkgurgul
11/14/2024, 4:18 PMdave08
11/14/2024, 4:20 PMclass SLambdaFake2<A, B, R>(var result: R) : suspend (A, B) -> R {
val calledWith = mutableListOf<Pair<A, B>>()
val results = mutableListOf<R>()
override suspend fun invoke(a: A, b: B): R {
calledWith.add(Pair(a, b))
results.add(result)
return result
}
fun reset() {
calledWith.clear()
}
}
class SLambdaFake3<A, B, C, R>(var result: R) : suspend (A, B, C) -> R {
val calledWith = mutableListOf<Triple<A, B, C>>()
val results = mutableListOf<R>()
override suspend fun invoke(a: A, b: B, c: C): R {
calledWith.add(Triple(a, b, c))
results.add(result)
return result
}
}
(my use cases are all fun interfaces with variable params)dave08
11/14/2024, 4:24 PMby
to create them in the Fake class and use a lambda to surround the function like:
class SomeFake() {
val fooTracker by tracker<String, Int, Foo>()
fun foo(f: String, b: Int): Foo = fooTracker(f, b) {
... return Foo
}
}
// In test
assert(someFake.fooTracker.result, Foo(...))
not the same implementation as I posted above, but it could be an easy-to-use tracker that could help in making fakes...dave08
11/14/2024, 4:25 PMdave08
11/14/2024, 4:29 PMevery {. ... }
blocks of a mocking framework...
It could be the code is a bit wrong, it's just an idea/general direction, but it could certainly be improved.Javier
11/14/2024, 4:36 PM