I have another question :wink: Why is that ``` c...
# mockk
s
I have another question 😉 Why is that
Copy code
coEvery { view.showDeleteSectionDialog() } returns true
produces
Copy code
Bad recording sequence. State: AnsweringState
but
Copy code
with(view) {
    coEvery { showDeleteSectionDialog() } returns true
}
does not?
Seems to be related to the coroutine-versions of the
verify
functions only
o
Should be just syntactic sugar
s
Is this intentional or a bug?
o
Bug
s
Okay 👍
o
But I am not able to understand how it may happen
Aw with should have same bytecode
As*
s
Should I create an issue on GitHub?
o
(the answer is yes 🙂 )
s
It seems that this was (un)intentionally fixed in MockK 1.9.1 😎
o
Good
s
Or... wait a minute. I have tests where it works and others where not. Have to investigate further...
This might not be a MockK problem at all. I'm using Spek 2.0 and I have something like this in my test class
Copy code
val mock by memoized {
    mockk<Cache<Entry>> {
        coEvery { tryGet("uuid") } returns entry1
    }
}
Now when I try to specify additional behaviour in a test with
coEvery { mock... }
I get this error whereas
with(mock) { coEvery { ... } }
works. When I remove
memoized
both variants works. It seems to be related to the
memoized
delegate.
@oleksiyp Do you have an idea what the problem could be or should I direct my issue to the developers of the Spek framework?
o
I think you shouldnt specify
mock
variable in the scope it is being initialized. Just add more behaviours without it
s
I don't understand. Usually I set up my tests like this. Declare mocks and common behaviour before tests and add special behaviour or overriding default behaviour in each test
Copy code
class Test : Spek(
  {
    val mock by memoized { 
      mockk<MyClass> {
        coEvery { doSomething() } returns SOMETHING
      } 
    }

    it("should do something") {
      coEvery { mock.doSomethingElse() } returns SOMETHING_ELSE

      // further test...
    }

    it("should do something else") {
      coEvery { mock.doSomethingElse() } returns null

      // further test...
    }
  })
o
Ok, now it is clear what you have where. I think yes it may be a case that you need to initialize mock before putting it to
every
. Nothing you can do about it
s
I still don't understand why
with(memoizedMock) { coEvery { ... } }
is different than
coEvery { memoizedMock... }
. Shouldn't the property be initialized when the property is accessed?
o
It initialize mock
s
So
coEvery { mock.something() }
assumes that
mock
has been initialized before the call of
coEvery
?
o
Yes, inside coEvery { magic happens }