Skip to content

Commit

Permalink
Merge pull request #1174 from Gosunet/master
Browse files Browse the repository at this point in the history
Fix wasNotshould throw exception when called on non mocked object
  • Loading branch information
Raibaz committed Feb 29, 2024
2 parents f8c9f13 + 8ea5be1 commit 1025c9b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
12 changes: 12 additions & 0 deletions modules/mockk-dsl/src/commonMain/kotlin/io/mockk/API.kt
Expand Up @@ -2166,6 +2166,18 @@ class MockKVerificationScope(

@Suppress("UNUSED_PARAMETER")
infix fun List<Any>.wasNot(called: Called) {
if (!all {
MockKDsl.internalIsMockKMock(
mock = it,
regular = true,
spy = true,
objectMock = true,
staticMock = true,
constructorMock = true
)
}) {
throw MockKException("was not can only be called on a mocked object")
}
callRecorder.wasNotCalled(this)
}
}
Expand Down
Expand Up @@ -25,9 +25,7 @@ class StubbingStateTest {
state.recordingDone()
}

verify {
recorder.factories.stubbingAwaitingAnswerState wasNot Called
}
verify(exactly = 0) { recorder.factories.stubbingAwaitingAnswerState(any()) }
}

@Test
Expand Down
@@ -1,6 +1,7 @@
package io.mockk.it

import io.mockk.*
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
Expand Down Expand Up @@ -86,6 +87,7 @@ class ConstructorMockTest {
}

@Test
@Ignore // TODO fix verify bug an anyConstructed https://github.com/mockk/mockk/issues/1224
fun clear() {
mockkConstructor(MockCls::class)

Expand All @@ -97,7 +99,7 @@ class ConstructorMockTest {

clearConstructorMockk(MockCls::class)

verify { anyConstructed<MockCls>() wasNot Called }
verify(exactly = 0) { anyConstructed<MockCls>() }

assertEquals(3, MockCls().op(1, 2))

Expand Down
@@ -0,0 +1,38 @@
package io.mockk.it

import io.mockk.*
import kotlinx.coroutines.flow.flowOf
import org.junit.jupiter.api.BeforeEach
import kotlin.test.Test
import kotlin.test.assertFailsWith

class WasNotCalledTest {

val mock = mockk<MockCls>()

@Test
fun wasNotCalledOnNonMockedObject() {
assertFailsWith<MockKException>("was not can should throw MockKException on non mock object") {
verify { mock.flowOp(1, 2) wasNot Called }
}
}

@Test
fun wasNotCalledOnMockedObject() {
verify { mock wasNot Called }
}

@Test
fun wasNotCalledShouldThrowAssertionErrorIfMockHasBeenCalled() {
assertFailsWith<AssertionError>("was not can should throw AssertionError if mock has been called") {
every { mock.flowOp(1, 2) } returns flowOf(1, 2)

mock.flowOp(1, 2)
verify { mock wasNot Called }
}
}

class MockCls {
fun flowOp(a: Int = 1, b: Int = 2) = flowOf(a, b)
}
}

0 comments on commit 1025c9b

Please sign in to comment.