Hello, I’m using mocKMP, want to say ty for nice p...
# kodein
d
Hello, I’m using mocKMP, want to say ty for nice plugin. I have a question, is it possible to override mock return value in the concrete unit test?
r
You mean something like this?
Copy code
mocker.every { db.loadUser("123") } returns null // first call
mocker.every { db.loadUser("456") } returns FakeUser() // second call
Or like this?
Copy code
mocker.every { db.loadUser("123") } returns null // first call
// Do something that triggers the first call
mocker.every { db.loadUser("123") } returns FakeUser() // second call
d
something like
Copy code
@BeforeTest
fun setup() { // should use library setup func.
   mocker.every { db.loadUser("123") } returns null // to bypass all test that in flow using that mock
}


@Test
fun test() {
 mocker.every { db.loadUser("123") } returns FakeUser() // specifically check how flow behaves with the return value

 // test logic
}
r
You can do this using a variable
Copy code
// Declare reusable mock
val everyUserLoad by lazy { mocker.every { db.loadUser("123") } }

@BeforeTest
fun setup() { // should use library setup func.
everyUserLoad returns null // to bypass all test that in flow using that mock
}


@Test
fun test() {
everyUserLoad returns FakeUser() // specifically check how flow behaves with the return value
// test logic
}
1
🙌 1
d
Thank you, will try that approach