1

Tryin to mock the android.content.pm.PackageInfo, and stub the versionName.

    @Test
    fun test_() {

        val pInfoMock = mockk<PackageInfo>()
        every { pInfoMock.versionName } returns "test_version". //<==got error
        // ......
    }

it got error:

Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
    at io.mockk.impl.recording.states.StubbingState.checkMissingCalls(StubbingState.kt:14)
    at io.mockk.impl.recording.states.StubbingState.recordingDone(StubbingState.kt:8)
    at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
    at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:64)
    at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
    at io.mockk.MockKDsl.internalEvery(API.kt:94)
    at io.mockk.MockKKt.every(MockK.kt:143)

what does this mean?

Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
ObscureCookie
  • 756
  • 2
  • 10
lannyf
  • 9,865
  • 12
  • 70
  • 152

1 Answers1

2

versionName is a property, not a method. so MockK cannot mock it with every. You need to assign "test_version" to versionName

@Test
fun test_() {
    val pInfoMock = mockk<PackageInfo>()
    pInfoMock.versionName = "test_version" // assign the version name
    // ......
}
ObscureCookie
  • 756
  • 2
  • 10