Hi team, I'm just upgrading my app to GB v2.0.0, a...
# sdk-kotlin
f
Hi team, I'm just upgrading my app to GB v2.0.0, and I ran into some issues. Can we rename one of these two functions into something different? The latest update is breaking our tests, since inline functions can't be mocked. When I try to mock the first method, sometimes it calls the second one instead, and it ends up calling the actual internal implementation of GrowthBookSDK instead of the mock.
a
Maybe it can help:
Copy code
val mockedResult = GBFeatureResult(
            gbValue = GBNumber(5),
            source = GBFeatureSource.defaultValue,
        );

every { mockGrowthBookSdk.feature(featureName) } returns mockedResult
f
It has the same problem 😕 I think when attempting to mock the non-inline
feature()
function, it's instead calling the inline one.
🥲 1
you can repro with this, just run it a couple of times (it alternates between passing and failing)
Copy code
@Test
fun `mock growthbook sdk feature`() {
    val featureResult = GBFeatureResult(gbValue = GBNumber(5), source = GBFeatureSource.defaultValue)
    val gb: GrowthBookSDK = mockk {
        every { feature("key") } returns featureResult
    }
    val value = (gb.feature("key").gbValue as? GBNumber)?.value
    assertThat(value).isEqualTo(5)
}
a
I reproduced. Really strange sometimes passing sometimes failing
The stacktrace is pointing to
forcedFeature
field
Firstly I tried to mock
forcedFeatures
in order to achieve it I changed visibility modifier. It seems like you have no ability to change visibility modifier, you have only ability to change the version of GB
image.png
and it solved the problem
I think that the root of problem is private visibility modifier (mockk doesn't see it). I removed mocking forcedFeatures but left public visibility modifier and the test always passes
So, our class was inappropriate for testing, I have fixed it in this commit. When v3.0.0-alpha is released your test will be passing. For now you can comment it
f
can you try renaming one of the
feature()
methods? that might be a better solution than exposing
forcedFeatures
😕
a
rename from
feature()
to what? why public visibility modifier is bad? No need for encapsulation for forcedFeatures (it is not something that can be broken). When a class has public dependencies, this class is suitable for testing
The setter for forcedFeatures that was before didn't do any checks
For me it was passing with Java 17 but fails with Java 11