Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mock the expect object class in KMM project #823

Open
qinwang1023 opened this issue May 18, 2022 · 2 comments
Open

How to mock the expect object class in KMM project #823

qinwang1023 opened this issue May 18, 2022 · 2 comments
Labels

Comments

@qinwang1023
Copy link

Expected Behavior

Be able to mock the expect object class in the KMM project

Current Behavior

In my shared module, I created a use case class that uses the expected object class to do things like read and write files. But when I follow the guide book(https://notwoods.github.io/mockk-guidebook/docs/mocking/static/) to mock the read and write operations, according to the debug result, it does not seem to excute the result of my mock instead of excuting the real operation.

Steps to Reproduce

  1. create an expect object class in common module and use case class

The part of the use case class:

class UseCase {
  fun needToTest{
    ...
   if(FileOperation.mvFile(scr,dest)){
    ...
   }
    ...
}
}

The example file operation class:

  expect object FileOperation {
    
    fun rdFile(path: String): List<Path>?
   
    fun mvFile(srcPath: String, destPath: String): Boolean?
     ....
  }

android Part

  actual object FileOperation {
       ......
     actual fun mvFile(srcPath:String, destPath: String): Boolean? {
       ......
     }
      .......
  }

ios part

   actual object FileOperation {
   .......
   .......
}
  1. Start to test the function of the use case

The example mockk method what I used currently

val useCaseTest = mock<UseCase>()
mockkObject(FileOperation)
every{ FileOperation.mvFile(srcPathMock, any())} returns true
when{
  useCaseTest.needToTest()
}

Context

  • MockK version: 1.12.2
  • OS: macOS 12.4
  • Kotlin version: 1.6.10
  • JDK version: open jdk 11
  • JUnit version: 5.8.2
  • Type of test: unit test
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
package io.mockk.gh

import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject

class Test {

    @Test
    fun test() {
        val useCaseTest = mock<UseCase>()
        mockkObject(FileOperation)
        every{ FileOperation.mvFile(srcPathMock, any())} returns true
        when {
          useCaseTest.needToTest()
          }
    }
}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------
@qinwang1023
Copy link
Author

Accoding to the debug results in my local, it doesn't seem to use the mock action I set up in the test function but instead does the real file operation.

@stale
Copy link

stale bot commented Aug 13, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as stale just ask to put an important label.

@stale stale bot added the stale label Aug 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant