无法使用 Robolectric 测试 kotlin 接口

问题描述

目前,我正在使用一个名为 Robolectric 的测试框架。我想知道测试接口以检查其值和方法的正确方法是什么。例如,在下面的示例中,我有一个名为 TestInterface 的接口。我想测试这个界面是否可以工作。所以我设置如下,通过将接口变成一个对象。这是一个错误方法吗?或者有更好的方法吗?我对测试不精通。因此,如果有一些示例或提示,那将非常有帮助。谢谢。

@RunWith(RobolectricTestRunner::class)
    class MySampleTest {
    
      @Test
      fun `test mysample `() {
        val testName = "Test Name"
        val userInput = "User Input"
    
        val testResults = object : TestInterface {
          override val testValue: String
            get() = testName
    
          override fun createTest(input: String): String {
            return userInput+input
          }
        }.createTest(userInput)
    
        assertTrue(keyPair.verify(userInput,testResults))
      }

解决方法

因为我们不是在谈论 UI,所以说“测试界面”让我感到困惑。我们只能为实现编写测试。否则我们可以测试什么?

您的项目中应该有一个或多个实现(如果没有,为什么还需要这个接口?),以便您可以测试它们。