verify
we capture
some parameters and inspect them in more detail.verify { foo.bar(withArg { it is MyClass }) }
seems to work. Is this the way to go?
friends, i have a webFlux coRouter endpoints which has a kotlin muLogging statement. i would like to coVerify logger.info is executed in my Mockk unit-test case. any help would be appreciated. `fun myRouter() = coRouter {
val logger = KotlinLogging.logger {}
"/events".nest {
POST { req ->
val event = req.awaitBody<Event>()
logger.info { event }
ok().bodyValueAndAwait("")
}
}
}`
Hi All,
I wanna ask about mocking android ViewModel.
I have a view model which having a few live data on it…
I tried to mock it by
val vm = mockk<CalibrationViewModel>(relaxed = true)
every { vm.getCalibrationId() } returns "mocked”
on these getCalibrationId()
fun getCalibrationId(): String? {
return _calibrationId.value // <— private live data object
}
but then I get an error like this:
Caused by: java.lang.IllegalAccessError: tried to access class androidx.lifecycle.LiveData$ObserverWrapper from class androidx.lifecycle.LiveData$Subclass0
I thought that mockk will completely stub all the methods and properties without actually calling the real things….
any suggestions on this?
thanks in advance.
Another questions,
it seems to be hard to mock the viewModel with the issues above, so instead of mocking it, I tried to use the real object from the ViewModel.
val mockCalibration = CalibrationCreated(….)
fragment.viewModel = CalibrationViewModel(appContext).apply {
setPitchAngle(22.3f)
setRollAngle(33.3f)
setExistingCalibration(mockCalibration)
}
println(fragment.viewModel.getRollAngle()) // 33.3f
println(fragment.viewModel.getPitchAngle()) // 22.3f
println(fragment.viewModel.getExistingCalibration()) // null, should be not null
but whenever I get these calibration from live data that hold its value, it always null…
here is the code inside these ViewModel
class CalibrationViewModel(application: Application) : AndroidViewModel(application) {
…
private val _existingCalibration = MutableLiveData<CalibrationCreated>()
fun setExistingCalibration(value: CalibrationCreated) {
_existingCalibration.postValue(value)
}
fun getExistingCalibration(): CalibrationCreated? {
return _existingCalibration.value
}
…
}
any suggestion really appreciated.
Thanks in advance...
Unable to build Kotlin project configuration
org.gradle.internal.resolve.ArtifactNotFoundException: Could not find mockk-1.10.6-samplessources.jar (io.mockk:mockk:1.10.6).
mockk-common
fixed it
doWork
method and having it do nothing
doWork
and run it manually as part of your test, then you can capture the lambda with a slot and then run it manually in your test
hello folks :wave:
given the function:
fun <T : Any> myFunction(foo: Foo, bar: Bar, response: KClass<T>): MyResponse<T> { do magic }
how can it be mocked to control the returned object?
I tried something like:
every { mockedClass.myFunction(any(), any(), ofType(SomeType::class) } returns SomeType()
but it didn't work... and using the type directly, myFunction(any(), any(), SomeType::class)
is never matched.
hey all — what's the best way to see if a function is being called with the right shape string? I have a function getRandomIdAndRunMethod
and I want to verify that I'm calling method
with the right shape random id
example code:
getRandomId() {
do server side stuff and return the id that (depending on other service) will be of shape xxxx-xxxx
}
method(id) {
sends an email to someone based on id
}
getRandomIdAndRunMethod() {}
I'm trying to run this in a verify block and getting the error: No other calls allowed in stdObjectAnswer than equals/hashCode/toString
Hey have a problem by using a foreach loop of an object which is extending a Map. I try to setup my mocking like this
val tableMock = mockk<MyTable>(relaxed = true, name = "My table Mock")
val tableEntryMock = mockk<MyEntry>(relaxed = true, name = "My entry Mock")
every { tableMock[any()] } returns tableEntryMock
But in my foreach, where I'm iteration over MyTable nothing happen because its size is 0, obviously. So I try:
val tableMock = mockk<MyTable>(relaxed = true, name = "My table Mock")
val tableEntryMock = mockk<MyEntry>(relaxed = true, name = "My entry Mock")
val expectedMap = mutableMapOf<Int, MyEntry>(1 to tableEntryMock)
every { tableMock.iterator() } answers expectedMap.iterator()
But the precompiler has a problem with my 'answers' :None of the following functions can be called with the arguments supplied.
answers(Answer<MutableIterator<MutableMap.MutableEntry<Int, MyTable>>>) defined in io.mockk.MockKStubScope
answers(MockKAnswerScope<MutableIterator<MutableMap.MutableEntry<Int, MyTable>>, MutableIterator<MutableMap.MutableEntry<Int, MyTable>>>.(Call) → MutableIterator<MutableMap.MutableEntry<Int, MyTable>>) defined in io.mockk.MockKStubScope
answers
takes a block, not a value
you should be doingevery { tableMock.iterator() } returns expectedMap.iterator()
or
every { tableMock.iterator() } answers { expectedMap.iterator() }
Thanks to @Raibaz this doesn't solves the problem fully, but brings me one step forward (was using 'answers' wrong). Problem is a different type of the Iterators.
verify (exactly =1)
and there's no problem when passing in a normal string as an argument, but it's saying was not called
when I use a list of strings as an argument. I've tried every combination of eq
and similar that I can think of, but it's still insisting that the function hasn't been called when it definitely has. I thought it could be a reference vs value thing, but the eq
thing negates that as an option. Any ideas?
was not called
.