代码之家  ›  专栏  ›  技术社区  ›  Mahdi-Malv

mock中的mock嵌套属性

  •  0
  • Mahdi-Malv  · 技术社区  · 5 年前

    我有一个函数,它在 DisplayMetrics 属于 Resources 属于 Context 班级:

    fun getIconForDevice(context: Context, iconUrl: String): String {
        val metrics = context.resources.displayMetrics
        var suffix = ""
        //below checks MUST be in this increasing order or it may failed
        if (metrics.densityDpi <= DisplayMetrics.DENSITY_MEDIUM)
            suffix = "-m"
        else if (metrics.densityDpi <= DisplayMetrics.DENSITY_HIGH)
            suffix = "-h"
        else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XHIGH)
            suffix = "-xh"
        else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XXHIGH || metrics.densityDpi > DisplayMetrics.DENSITY_XXHIGH)
            suffix = "-xxh"
        val pasvand = iconUrl.substring(iconUrl.lastIndexOf("."))
        val str = iconUrl.substring(0, iconUrl.lastIndexOf(".")) + suffix + pasvand
        return str
    }
    

    上下文 metrics.densityDpi
    我在用 模拟(1.9.3) 去图书馆。

    @Test
    fun getIconForDevice_ReturnsUrlWithXxhForXxhDisplay() {
        val context: Context = mockk(relaxed = true)
        every { context.resources.displayMetrics.densityDpi } returns 450
        assertEquals(IconHelper.getIconForDevice(context,
          "https://website.com/image.png"), "https://website.com/image-xxh.png")
    }
    

    运行测试时出现以下错误:

    java.lang.ClassCastException: java.lang.Integer cannot be cast to android.util.DisplayMetrics
    
        at ...Resources.getDisplayMetrics(Resources.java)
        at ....IconHelper.getIconForDevice(IconHelper.kt:39)
        at ...IconHelperTest.getIconForDevice_ReturnsUrlWithXxhForXxhDisplay(IconHelperTest.kt:30)
    

    val metrics = context.resources.displayMetrics 实际函数的行

    那么,如何在mockk中模拟这样一个嵌套字段呢?
    context.resources.displayMetrics.densityDpi

    0 回复  |  直到 5 年前
        1
  •  1
  •   ChristianB    5 年前

    你的嘲笑计划 Context densityDpi 是正确的。

    测试失败,因为您正在呼叫 getIconForDevice 用一个新的mock代替你的mock 上下文 .

    @Test
    fun getIconForDevice_ReturnsUrlWithXxhForXxhDisplay() {
        val context: Context = mockk(relaxed = true)
        every { context.resources.displayMetrics.densityDpi } returns 450
    
        // pass in mocked context
        assertEquals(IconHelper.getIconForDevice(context, "https://website.com/image.png"), "https://website.com/image-xxh.png")
    }
    

    更新

    ClassCastException DisplayMetrics 第一:

    val metrics = context.resources.displayMetrics
    

    我想你得把这两个字分开 displayMetrics 密度dpi :

    @Test
    fun getIconForDevice_ReturnsUrlWithXxhForXxhDisplay() {
      val context: Context = mockk(relaxed = true)
      val displayMetrics: DisplayMetrics = mockk(relaxed = true) // maybe relaxed is not needed, I just put it here in case
      every { context.resources.displayMetrics } returns displayMetrics
      every { displayMetrics.densityDpi } returns 450
    
      assertEquals(IconHelper.getIconForDevice(context, "https://website.com/image.png"), "https://website.com/image-xxh.png")
    }
    
    推荐文章