kotlin 1.2.51
У меня есть следующие общие настройки, в которых используется общая функция расширения.
class SharedUserPreferencesImp(private val context: Context,
private val sharedPreferenceName: String): SharedUserPreferences {
private val sharedPreferences: SharedPreferences by lazy {
context.getSharedPreferences(sharedPreferenceName, Context.MODE_PRIVATE)
}
override fun <T : Any> T.getValue(key: String): T {
with(sharedPreferences) {
val result: Any = when (this@getValue) {
is String -> getString(key, this@getValue)
is Boolean -> getBoolean(key, this@getValue)
is Int -> getInt(key, this@getValue)
is Long -> getLong(key, this@getValue)
is Float -> getFloat(key, this@getValue)
else -> {
throw UnsupportedOperationException("Cannot find preference casting error")
}
}
@Suppress("unchecked_cast")
return result as T
}
}
}
Я пытаюсь написать модульный тест для этого метода. Как вы можете видеть в моем методе испытаний, testName.getValue("key")
getValue
не распознается.
class SharedUserPreferencesImpTest {
private lateinit var sharedUserPreferences: SharedUserPreferences
private val context: Context = mock()
@Before
fun setUp() {
sharedUserPreferences = SharedUserPreferencesImp(context, "sharedPreferenceName")
assertThat(sharedUserPreferences).isNotNull
}
@Test
fun `should get a string value from shared preferences`() {
val testName = "this is a test"
testName.getValue("key")
}
}
Как лучше всего протестировать функцию расширения, имеющую универсальный тип?
Большое спасибо за любые предложения,