代码之家  ›  专栏  ›  技术社区  ›  ant2009

如何对具有泛型类型的kotlin扩展函数进行单元测试

  •  0
  • ant2009  · 技术社区  · 7 年前
    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")
        }
    }
    

    测试具有泛型类型的扩展函数的最佳方法是什么?

    非常感谢您的建议,

    0 回复  |  直到 7 年前
        1
  •  1
  •   Hiosdra    7 年前

    两者之间存在冲突 T.getValue(key: String) 作为扩展函数和SharedUserReferenceImp成员函数。 你可以 T.getValue(键:字符串) 高级函数,这就解决了一个问题。下面是示例代码:

    fun <T : Any> T.getValue(key: String, sharedPreferences: SharedUserPreferencesImp): T {
        with(sharedPreferences.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
        }
    }
    
    class SharedUserPreferencesImp(private val context: Context,
                                   private val sharedPreferenceName: String): SharedUserPreferences {
    
        val sharedPreferences: SharedPreferences by lazy {
            context.getSharedPreferences(sharedPreferenceName, Context.MODE_PRIVATE)
        }
    }
    

    你也可以看看这两个很棒的库: https://github.com/chibatching/Kotpref https://github.com/MarcinMoskala/PreferenceHolder